1 // SPDX-License-Identifier: GPL-2.0-or-later
3 cx231xx-core.c - driver for Conexant Cx23100/101/102
4 USB video capture devices
6 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <media/v4l2-common.h>
18 #include <media/tuner.h>
20 #include "cx231xx-reg.h"
22 /* #define ENABLE_DEBUG_ISOC_FRAMES */
24 static unsigned int core_debug
;
25 module_param(core_debug
, int, 0644);
26 MODULE_PARM_DESC(core_debug
, "enable debug messages [core]");
28 #define cx231xx_coredbg(fmt, arg...) do {\
30 printk(KERN_INFO "%s %s :"fmt, \
31 dev->name, __func__ , ##arg); } while (0)
33 static unsigned int reg_debug
;
34 module_param(reg_debug
, int, 0644);
35 MODULE_PARM_DESC(reg_debug
, "enable debug messages [URB reg]");
37 static int alt
= CX231XX_PINOUT
;
38 module_param(alt
, int, 0644);
39 MODULE_PARM_DESC(alt
, "alternate setting to use for video endpoint");
41 #define cx231xx_isocdbg(fmt, arg...) do {\
43 printk(KERN_INFO "%s %s :"fmt, \
44 dev->name, __func__ , ##arg); } while (0)
46 /*****************************************************************
47 * Device control list functions *
48 ******************************************************************/
50 LIST_HEAD(cx231xx_devlist
);
51 static DEFINE_MUTEX(cx231xx_devlist_mutex
);
54 * cx231xx_realease_resources()
55 * unregisters the v4l2,i2c and usb devices
56 * called when the device gets disconnected or at module unload
58 void cx231xx_remove_from_devlist(struct cx231xx
*dev
)
62 if (dev
->udev
== NULL
)
65 if (atomic_read(&dev
->devlist_count
) > 0) {
66 mutex_lock(&cx231xx_devlist_mutex
);
67 list_del(&dev
->devlist
);
68 atomic_dec(&dev
->devlist_count
);
69 mutex_unlock(&cx231xx_devlist_mutex
);
73 void cx231xx_add_into_devlist(struct cx231xx
*dev
)
75 mutex_lock(&cx231xx_devlist_mutex
);
76 list_add_tail(&dev
->devlist
, &cx231xx_devlist
);
77 atomic_inc(&dev
->devlist_count
);
78 mutex_unlock(&cx231xx_devlist_mutex
);
81 static LIST_HEAD(cx231xx_extension_devlist
);
83 int cx231xx_register_extension(struct cx231xx_ops
*ops
)
85 struct cx231xx
*dev
= NULL
;
87 mutex_lock(&cx231xx_devlist_mutex
);
88 list_add_tail(&ops
->next
, &cx231xx_extension_devlist
);
89 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
91 dev_info(dev
->dev
, "%s initialized\n", ops
->name
);
93 mutex_unlock(&cx231xx_devlist_mutex
);
96 EXPORT_SYMBOL(cx231xx_register_extension
);
98 void cx231xx_unregister_extension(struct cx231xx_ops
*ops
)
100 struct cx231xx
*dev
= NULL
;
102 mutex_lock(&cx231xx_devlist_mutex
);
103 list_for_each_entry(dev
, &cx231xx_devlist
, devlist
) {
105 dev_info(dev
->dev
, "%s removed\n", ops
->name
);
108 list_del(&ops
->next
);
109 mutex_unlock(&cx231xx_devlist_mutex
);
111 EXPORT_SYMBOL(cx231xx_unregister_extension
);
113 void cx231xx_init_extension(struct cx231xx
*dev
)
115 struct cx231xx_ops
*ops
= NULL
;
117 mutex_lock(&cx231xx_devlist_mutex
);
118 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
122 mutex_unlock(&cx231xx_devlist_mutex
);
125 void cx231xx_close_extension(struct cx231xx
*dev
)
127 struct cx231xx_ops
*ops
= NULL
;
129 mutex_lock(&cx231xx_devlist_mutex
);
130 list_for_each_entry(ops
, &cx231xx_extension_devlist
, next
) {
134 mutex_unlock(&cx231xx_devlist_mutex
);
137 /****************************************************************
138 * U S B related functions *
139 *****************************************************************/
140 int cx231xx_send_usb_command(struct cx231xx_i2c
*i2c_bus
,
141 struct cx231xx_i2c_xfer_data
*req_data
)
144 struct cx231xx
*dev
= i2c_bus
->dev
;
145 struct VENDOR_REQUEST_IN ven_req
;
152 if (dev
->state
& DEV_DISCONNECTED
)
155 /* Get the I2C period, nostop and reserve parameters */
156 _i2c_period
= i2c_bus
->i2c_period
;
157 _i2c_nostop
= i2c_bus
->i2c_nostop
;
158 _i2c_reserve
= i2c_bus
->i2c_reserve
;
160 saddr_len
= req_data
->saddr_len
;
163 ven_req
.wValue
= (req_data
->dev_addr
<< 9 | _i2c_period
<< 4 |
164 saddr_len
<< 2 | _i2c_nostop
<< 1 | I2C_SYNC
|
167 /* set channel number */
168 if (req_data
->direction
& I2C_M_RD
) {
169 /* channel number, for read,spec required channel_num +4 */
170 ven_req
.bRequest
= i2c_bus
->nr
+ 4;
172 ven_req
.bRequest
= i2c_bus
->nr
; /* channel number, */
174 /* set index value */
177 ven_req
.wIndex
= 0; /* need check */
180 ven_req
.wIndex
= (req_data
->saddr_dat
& 0xff);
183 ven_req
.wIndex
= req_data
->saddr_dat
;
187 /* set wLength value */
188 ven_req
.wLength
= req_data
->buf_size
;
190 /* set bData value */
193 /* set the direction */
194 if (req_data
->direction
) {
195 ven_req
.direction
= USB_DIR_IN
;
196 memset(req_data
->p_buffer
, 0x00, ven_req
.wLength
);
198 ven_req
.direction
= USB_DIR_OUT
;
200 /* set the buffer for read / write */
201 ven_req
.pBuff
= req_data
->p_buffer
;
204 /* call common vendor command request */
205 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
206 if (status
< 0 && !dev
->i2c_scan_running
) {
207 dev_err(dev
->dev
, "%s: failed with status -%d\n",
213 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command
);
216 * Sends/Receives URB control messages, assuring to use a kalloced buffer
217 * for all operations (dev->urb_buf), to avoid using stacked buffers, as
218 * they aren't safe for usage with USB, due to DMA restrictions.
219 * Also implements the debug code for control URB's.
221 static int __usb_control_msg(struct cx231xx
*dev
, unsigned int pipe
,
222 __u8 request
, __u8 requesttype
, __u16 value
, __u16 index
,
223 void *data
, __u16 size
, int timeout
)
228 printk(KERN_DEBUG
"%s: (pipe 0x%08x): %s: %02x %02x %02x %02x %02x %02x %02x %02x ",
231 (requesttype
& USB_DIR_IN
) ? "IN" : "OUT",
234 value
& 0xff, value
>> 8,
235 index
& 0xff, index
>> 8,
236 size
& 0xff, size
>> 8);
237 if (!(requesttype
& USB_DIR_IN
)) {
238 printk(KERN_CONT
">>>");
239 for (i
= 0; i
< size
; i
++)
240 printk(KERN_CONT
" %02x",
241 ((unsigned char *)data
)[i
]);
245 /* Do the real call to usb_control_msg */
246 mutex_lock(&dev
->ctrl_urb_lock
);
247 if (!(requesttype
& USB_DIR_IN
) && size
)
248 memcpy(dev
->urb_buf
, data
, size
);
249 rc
= usb_control_msg(dev
->udev
, pipe
, request
, requesttype
, value
,
250 index
, dev
->urb_buf
, size
, timeout
);
251 if ((requesttype
& USB_DIR_IN
) && size
)
252 memcpy(data
, dev
->urb_buf
, size
);
253 mutex_unlock(&dev
->ctrl_urb_lock
);
256 if (unlikely(rc
< 0)) {
257 printk(KERN_CONT
"FAILED!\n");
261 if ((requesttype
& USB_DIR_IN
)) {
262 printk(KERN_CONT
"<<<");
263 for (i
= 0; i
< size
; i
++)
264 printk(KERN_CONT
" %02x",
265 ((unsigned char *)data
)[i
]);
267 printk(KERN_CONT
"\n");
275 * cx231xx_read_ctrl_reg()
276 * reads data from the usb device specifying bRequest and wValue
278 int cx231xx_read_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
,
283 int pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
285 if (dev
->state
& DEV_DISCONNECTED
)
288 if (len
> URB_MAX_CTRL_SIZE
)
293 val
= ENABLE_ONE_BYTE
;
296 val
= ENABLE_TWE_BYTE
;
299 val
= ENABLE_THREE_BYTE
;
302 val
= ENABLE_FOUR_BYTE
;
305 val
= 0xFF; /* invalid option */
311 ret
= __usb_control_msg(dev
, pipe
, req
,
312 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
313 val
, reg
, buf
, len
, HZ
);
317 int cx231xx_send_vendor_cmd(struct cx231xx
*dev
,
318 struct VENDOR_REQUEST_IN
*ven_req
)
325 if (dev
->state
& DEV_DISCONNECTED
)
328 if ((ven_req
->wLength
> URB_MAX_CTRL_SIZE
))
331 if (ven_req
->direction
)
332 pipe
= usb_rcvctrlpipe(dev
->udev
, 0);
334 pipe
= usb_sndctrlpipe(dev
->udev
, 0);
337 * If the cx23102 read more than 4 bytes with i2c bus,
338 * need chop to 4 byte per request
340 if ((ven_req
->wLength
> 4) && ((ven_req
->bRequest
== 0x4) ||
341 (ven_req
->bRequest
== 0x5) ||
342 (ven_req
->bRequest
== 0x6) ||
344 /* Internal Master 3 Bus can send
345 * and receive only 4 bytes per time
347 (ven_req
->bRequest
== 0x2))) {
349 pdata
= ven_req
->pBuff
;
352 unsend_size
= ven_req
->wLength
;
354 /* the first package */
355 ven_req
->wValue
= ven_req
->wValue
& 0xFFFB;
356 ven_req
->wValue
= (ven_req
->wValue
& 0xFFBD) | 0x2;
357 ret
= __usb_control_msg(dev
, pipe
, ven_req
->bRequest
,
358 ven_req
->direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
359 ven_req
->wValue
, ven_req
->wIndex
, pdata
,
361 unsend_size
= unsend_size
- 4;
363 /* the middle package */
364 ven_req
->wValue
= (ven_req
->wValue
& 0xFFBD) | 0x42;
365 while (unsend_size
- 4 > 0) {
367 ret
= __usb_control_msg(dev
, pipe
,
369 ven_req
->direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
370 ven_req
->wValue
, ven_req
->wIndex
, pdata
,
372 unsend_size
= unsend_size
- 4;
375 /* the last package */
376 ven_req
->wValue
= (ven_req
->wValue
& 0xFFBD) | 0x40;
378 ret
= __usb_control_msg(dev
, pipe
, ven_req
->bRequest
,
379 ven_req
->direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
380 ven_req
->wValue
, ven_req
->wIndex
, pdata
,
383 ret
= __usb_control_msg(dev
, pipe
, ven_req
->bRequest
,
384 ven_req
->direction
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
385 ven_req
->wValue
, ven_req
->wIndex
,
386 ven_req
->pBuff
, ven_req
->wLength
, HZ
);
393 * cx231xx_write_ctrl_reg()
394 * sends data to the usb device, specifying bRequest
396 int cx231xx_write_ctrl_reg(struct cx231xx
*dev
, u8 req
, u16 reg
, char *buf
,
401 int pipe
= usb_sndctrlpipe(dev
->udev
, 0);
403 if (dev
->state
& DEV_DISCONNECTED
)
406 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
411 val
= ENABLE_ONE_BYTE
;
414 val
= ENABLE_TWE_BYTE
;
417 val
= ENABLE_THREE_BYTE
;
420 val
= ENABLE_FOUR_BYTE
;
423 val
= 0xFF; /* invalid option */
432 cx231xx_isocdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
434 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
435 req
, 0, val
, reg
& 0xff,
436 reg
>> 8, len
& 0xff, len
>> 8);
438 for (byte
= 0; byte
< len
; byte
++)
439 cx231xx_isocdbg(" %02x", (unsigned char)buf
[byte
]);
440 cx231xx_isocdbg("\n");
443 ret
= __usb_control_msg(dev
, pipe
, req
,
444 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
445 val
, reg
, buf
, len
, HZ
);
450 /****************************************************************
451 * USB Alternate Setting functions *
452 *****************************************************************/
454 int cx231xx_set_video_alternate(struct cx231xx
*dev
)
456 int errCode
, prev_alt
= dev
->video_mode
.alt
;
457 unsigned int min_pkt_size
= dev
->width
* 2 + 4;
458 u32 usb_interface_index
= 0;
460 /* When image size is bigger than a certain value,
461 the frame size should be increased, otherwise, only
462 green screen will be received.
464 if (dev
->width
* 2 * dev
->height
> 720 * 240 * 2)
467 if (dev
->width
> 360) {
468 /* resolutions: 720,704,640 */
469 dev
->video_mode
.alt
= 3;
470 } else if (dev
->width
> 180) {
471 /* resolutions: 360,352,320,240 */
472 dev
->video_mode
.alt
= 2;
473 } else if (dev
->width
> 0) {
474 /* resolutions: 180,176,160,128,88 */
475 dev
->video_mode
.alt
= 1;
477 /* Change to alt0 BULK to release USB bandwidth */
478 dev
->video_mode
.alt
= 0;
481 if (dev
->USE_ISO
== 0)
482 dev
->video_mode
.alt
= 0;
484 cx231xx_coredbg("dev->video_mode.alt= %d\n", dev
->video_mode
.alt
);
486 /* Get the correct video interface Index */
487 usb_interface_index
=
488 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
491 if (dev
->video_mode
.alt
!= prev_alt
) {
492 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
493 min_pkt_size
, dev
->video_mode
.alt
);
495 if (dev
->video_mode
.alt_max_pkt_size
!= NULL
)
496 dev
->video_mode
.max_pkt_size
=
497 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.alt
];
498 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
500 dev
->video_mode
.max_pkt_size
);
502 usb_set_interface(dev
->udev
, usb_interface_index
,
503 dev
->video_mode
.alt
);
506 "cannot change alt number to %d (error=%i)\n",
507 dev
->video_mode
.alt
, errCode
);
514 int cx231xx_set_alt_setting(struct cx231xx
*dev
, u8 index
, u8 alt
)
517 u32 usb_interface_index
= 0;
518 u32 max_pkt_size
= 0;
522 usb_interface_index
=
523 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
525 dev
->ts1_mode
.alt
= alt
;
526 if (dev
->ts1_mode
.alt_max_pkt_size
!= NULL
)
527 max_pkt_size
= dev
->ts1_mode
.max_pkt_size
=
528 dev
->ts1_mode
.alt_max_pkt_size
[dev
->ts1_mode
.alt
];
531 usb_interface_index
=
532 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
536 usb_interface_index
=
537 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
540 if (dev
->adev
.alt_max_pkt_size
!= NULL
)
541 max_pkt_size
= dev
->adev
.max_pkt_size
=
542 dev
->adev
.alt_max_pkt_size
[dev
->adev
.alt
];
545 usb_interface_index
=
546 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
548 dev
->video_mode
.alt
= alt
;
549 if (dev
->video_mode
.alt_max_pkt_size
!= NULL
)
550 max_pkt_size
= dev
->video_mode
.max_pkt_size
=
551 dev
->video_mode
.alt_max_pkt_size
[dev
->video_mode
.
555 if (dev
->board
.no_alt_vanc
)
557 usb_interface_index
=
558 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
560 dev
->vbi_mode
.alt
= alt
;
561 if (dev
->vbi_mode
.alt_max_pkt_size
!= NULL
)
562 max_pkt_size
= dev
->vbi_mode
.max_pkt_size
=
563 dev
->vbi_mode
.alt_max_pkt_size
[dev
->vbi_mode
.alt
];
566 usb_interface_index
=
567 dev
->current_pcb_config
.hs_config_info
[0].interface_info
.
569 dev
->sliced_cc_mode
.alt
= alt
;
570 if (dev
->sliced_cc_mode
.alt_max_pkt_size
!= NULL
)
571 max_pkt_size
= dev
->sliced_cc_mode
.max_pkt_size
=
572 dev
->sliced_cc_mode
.alt_max_pkt_size
[dev
->
580 if (alt
> 0 && max_pkt_size
== 0) {
582 "can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
583 usb_interface_index
, alt
);
584 /*To workaround error number=-71 on EP0 for videograbber,
585 need add following codes.*/
586 if (dev
->board
.no_alt_vanc
)
590 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,Interface = %d\n",
592 usb_interface_index
);
594 if (usb_interface_index
> 0) {
595 status
= usb_set_interface(dev
->udev
, usb_interface_index
, alt
);
598 "can't change interface %d alt no. to %d (err=%i)\n",
599 usb_interface_index
, alt
, status
);
606 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting
);
608 int cx231xx_gpio_set(struct cx231xx
*dev
, struct cx231xx_reg_seq
*gpio
)
615 /* Send GPIO reset sequences specified at board entry */
616 while (gpio
->sleep
>= 0) {
617 rc
= cx231xx_set_gpio_value(dev
, gpio
->bit
, gpio
->val
);
629 int cx231xx_demod_reset(struct cx231xx
*dev
)
633 u8 value
[4] = { 0, 0, 0, 0 };
635 status
= cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, PWR_CTL_EN
,
638 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN
,
639 value
[0], value
[1], value
[2], value
[3]);
641 cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
644 status
= cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
645 PWR_CTL_EN
, value
, 4);
649 status
= cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
650 PWR_CTL_EN
, value
, 4);
654 status
= cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
655 PWR_CTL_EN
, value
, 4);
658 status
= cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, PWR_CTL_EN
,
661 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN
,
662 value
[0], value
[1], value
[2], value
[3]);
666 EXPORT_SYMBOL_GPL(cx231xx_demod_reset
);
667 int is_fw_load(struct cx231xx
*dev
)
669 return cx231xx_check_fw(dev
);
671 EXPORT_SYMBOL_GPL(is_fw_load
);
673 int cx231xx_set_mode(struct cx231xx
*dev
, enum cx231xx_mode set_mode
)
677 if (dev
->mode
== set_mode
)
680 if (set_mode
== CX231XX_SUSPEND
) {
681 /* Set the chip in power saving mode */
682 dev
->mode
= set_mode
;
685 /* Resource is locked */
686 if (dev
->mode
!= CX231XX_SUSPEND
)
689 dev
->mode
= set_mode
;
691 if (dev
->mode
== CX231XX_DIGITAL_MODE
)/* Set Digital power mode */ {
692 /* set AGC mode to Digital */
693 switch (dev
->model
) {
694 case CX231XX_BOARD_CNXT_CARRAERA
:
695 case CX231XX_BOARD_CNXT_RDE_250
:
696 case CX231XX_BOARD_CNXT_SHELBY
:
697 case CX231XX_BOARD_CNXT_RDU_250
:
698 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 0);
700 case CX231XX_BOARD_CNXT_RDE_253S
:
701 case CX231XX_BOARD_CNXT_RDU_253S
:
702 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID
:
703 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
705 case CX231XX_BOARD_HAUPPAUGE_EXETER
:
706 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx
:
707 errCode
= cx231xx_set_power_mode(dev
,
708 POLARIS_AVMODE_DIGITAL
);
713 } else/* Set Analog Power mode */ {
714 /* set AGC mode to Analog */
715 switch (dev
->model
) {
716 case CX231XX_BOARD_CNXT_CARRAERA
:
717 case CX231XX_BOARD_CNXT_RDE_250
:
718 case CX231XX_BOARD_CNXT_SHELBY
:
719 case CX231XX_BOARD_CNXT_RDU_250
:
720 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
722 case CX231XX_BOARD_CNXT_RDE_253S
:
723 case CX231XX_BOARD_CNXT_RDU_253S
:
724 case CX231XX_BOARD_HAUPPAUGE_EXETER
:
725 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx
:
726 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID
:
727 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL
:
728 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC
:
729 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 0);
737 dev_err(dev
->dev
, "Failed to set devmode to %s: error: %i",
738 dev
->mode
== CX231XX_DIGITAL_MODE
? "digital" : "analog",
745 EXPORT_SYMBOL_GPL(cx231xx_set_mode
);
747 int cx231xx_ep5_bulkout(struct cx231xx
*dev
, u8
*firmware
, u16 size
)
754 buffer
= kzalloc(4096, GFP_KERNEL
);
757 memcpy(&buffer
[0], firmware
, 4096);
759 ret
= usb_bulk_msg(dev
->udev
, usb_sndbulkpipe(dev
->udev
, 5),
760 buffer
, 4096, &actlen
, 2000);
764 "bulk message failed: %d (%d/%d)", ret
,
767 errCode
= actlen
!= size
? -1 : 0;
773 /*****************************************************************
774 * URB Streaming functions *
775 ******************************************************************/
778 * IRQ callback, called by URB callback
780 static void cx231xx_isoc_irq_callback(struct urb
*urb
)
782 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
783 struct cx231xx_video_mode
*vmode
=
784 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
785 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, video_mode
);
789 switch (urb
->status
) {
790 case 0: /* success */
791 case -ETIMEDOUT
: /* NAK */
793 case -ECONNRESET
: /* kill */
798 cx231xx_isocdbg("urb completion error %d.\n", urb
->status
);
802 /* Copy data from URB */
803 spin_lock_irqsave(&dev
->video_mode
.slock
, flags
);
804 dev
->video_mode
.isoc_ctl
.isoc_copy(dev
, urb
);
805 spin_unlock_irqrestore(&dev
->video_mode
.slock
, flags
);
807 /* Reset urb buffers */
808 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
809 urb
->iso_frame_desc
[i
].status
= 0;
810 urb
->iso_frame_desc
[i
].actual_length
= 0;
813 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
815 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
819 /*****************************************************************
820 * URB Streaming functions *
821 ******************************************************************/
824 * IRQ callback, called by URB callback
826 static void cx231xx_bulk_irq_callback(struct urb
*urb
)
828 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
829 struct cx231xx_video_mode
*vmode
=
830 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
831 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, video_mode
);
834 switch (urb
->status
) {
835 case 0: /* success */
836 case -ETIMEDOUT
: /* NAK */
838 case -ECONNRESET
: /* kill */
842 case -EPIPE
: /* stall */
843 cx231xx_isocdbg("urb completion error - device is stalled.\n");
846 cx231xx_isocdbg("urb completion error %d.\n", urb
->status
);
850 /* Copy data from URB */
851 spin_lock_irqsave(&dev
->video_mode
.slock
, flags
);
852 dev
->video_mode
.bulk_ctl
.bulk_copy(dev
, urb
);
853 spin_unlock_irqrestore(&dev
->video_mode
.slock
, flags
);
855 /* Reset urb buffers */
856 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
858 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
863 * Stop and Deallocate URBs
865 void cx231xx_uninit_isoc(struct cx231xx
*dev
)
867 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
870 bool broken_pipe
= false;
872 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
874 dev
->video_mode
.isoc_ctl
.nfields
= -1;
875 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
876 urb
= dev
->video_mode
.isoc_ctl
.urb
[i
];
878 if (!irqs_disabled())
883 if (dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
884 usb_free_coherent(dev
->udev
,
885 urb
->transfer_buffer_length
,
886 dev
->video_mode
.isoc_ctl
.
890 if (urb
->status
== -EPIPE
) {
894 dev
->video_mode
.isoc_ctl
.urb
[i
] = NULL
;
896 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] = NULL
;
900 cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
901 usb_reset_endpoint(dev
->udev
, dev
->video_mode
.end_point_addr
);
903 kfree(dev
->video_mode
.isoc_ctl
.urb
);
904 kfree(dev
->video_mode
.isoc_ctl
.transfer_buffer
);
905 kfree(dma_q
->p_left_data
);
907 dev
->video_mode
.isoc_ctl
.urb
= NULL
;
908 dev
->video_mode
.isoc_ctl
.transfer_buffer
= NULL
;
909 dev
->video_mode
.isoc_ctl
.num_bufs
= 0;
910 dma_q
->p_left_data
= NULL
;
912 if (dev
->mode_tv
== 0)
913 cx231xx_capture_start(dev
, 0, Raw_Video
);
915 cx231xx_capture_start(dev
, 0, TS1_serial_mode
);
919 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc
);
922 * Stop and Deallocate URBs
924 void cx231xx_uninit_bulk(struct cx231xx
*dev
)
926 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
929 bool broken_pipe
= false;
931 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
933 dev
->video_mode
.bulk_ctl
.nfields
= -1;
934 for (i
= 0; i
< dev
->video_mode
.bulk_ctl
.num_bufs
; i
++) {
935 urb
= dev
->video_mode
.bulk_ctl
.urb
[i
];
937 if (!irqs_disabled())
942 if (dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
]) {
943 usb_free_coherent(dev
->udev
,
944 urb
->transfer_buffer_length
,
945 dev
->video_mode
.bulk_ctl
.
949 if (urb
->status
== -EPIPE
) {
953 dev
->video_mode
.bulk_ctl
.urb
[i
] = NULL
;
955 dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
] = NULL
;
959 cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
960 usb_reset_endpoint(dev
->udev
, dev
->video_mode
.end_point_addr
);
962 kfree(dev
->video_mode
.bulk_ctl
.urb
);
963 kfree(dev
->video_mode
.bulk_ctl
.transfer_buffer
);
964 kfree(dma_q
->p_left_data
);
966 dev
->video_mode
.bulk_ctl
.urb
= NULL
;
967 dev
->video_mode
.bulk_ctl
.transfer_buffer
= NULL
;
968 dev
->video_mode
.bulk_ctl
.num_bufs
= 0;
969 dma_q
->p_left_data
= NULL
;
971 if (dev
->mode_tv
== 0)
972 cx231xx_capture_start(dev
, 0, Raw_Video
);
974 cx231xx_capture_start(dev
, 0, TS1_serial_mode
);
978 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk
);
981 * Allocate URBs and start IRQ
983 int cx231xx_init_isoc(struct cx231xx
*dev
, int max_packets
,
984 int num_bufs
, int max_pkt_size
,
985 int (*isoc_copy
) (struct cx231xx
*dev
, struct urb
*urb
))
987 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
994 /* De-allocates all pending stuff */
995 cx231xx_uninit_isoc(dev
);
997 dma_q
->p_left_data
= kzalloc(4096, GFP_KERNEL
);
998 if (dma_q
->p_left_data
== NULL
)
1001 dev
->video_mode
.isoc_ctl
.isoc_copy
= isoc_copy
;
1002 dev
->video_mode
.isoc_ctl
.num_bufs
= num_bufs
;
1004 dma_q
->is_partial_line
= 0;
1005 dma_q
->last_sav
= 0;
1006 dma_q
->current_field
= -1;
1007 dma_q
->field1_done
= 0;
1008 dma_q
->lines_per_field
= dev
->height
/ 2;
1009 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
1010 dma_q
->lines_completed
= 0;
1011 dma_q
->mpeg_buffer_done
= 0;
1012 dma_q
->left_data_count
= 0;
1013 dma_q
->mpeg_buffer_completed
= 0;
1014 dma_q
->add_ps_package_head
= CX231XX_NEED_ADD_PS_PACKAGE_HEAD
;
1015 dma_q
->ps_head
[0] = 0x00;
1016 dma_q
->ps_head
[1] = 0x00;
1017 dma_q
->ps_head
[2] = 0x01;
1018 dma_q
->ps_head
[3] = 0xBA;
1019 for (i
= 0; i
< 8; i
++)
1020 dma_q
->partial_buf
[i
] = 0;
1022 dev
->video_mode
.isoc_ctl
.urb
=
1023 kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
1024 if (!dev
->video_mode
.isoc_ctl
.urb
) {
1026 "cannot alloc memory for usb buffers\n");
1030 dev
->video_mode
.isoc_ctl
.transfer_buffer
=
1031 kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
1032 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
) {
1034 "cannot allocate memory for usbtransfer\n");
1035 kfree(dev
->video_mode
.isoc_ctl
.urb
);
1039 dev
->video_mode
.isoc_ctl
.max_pkt_size
= max_pkt_size
;
1040 dev
->video_mode
.isoc_ctl
.buf
= NULL
;
1042 sb_size
= max_packets
* dev
->video_mode
.isoc_ctl
.max_pkt_size
;
1044 if (dev
->mode_tv
== 1)
1045 dev
->video_mode
.end_point_addr
= 0x81;
1047 dev
->video_mode
.end_point_addr
= 0x84;
1050 /* allocate urbs and transfer buffers */
1051 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
1052 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
1054 cx231xx_uninit_isoc(dev
);
1057 dev
->video_mode
.isoc_ctl
.urb
[i
] = urb
;
1059 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
] =
1060 usb_alloc_coherent(dev
->udev
, sb_size
, GFP_KERNEL
,
1061 &urb
->transfer_dma
);
1062 if (!dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
]) {
1064 "unable to allocate %i bytes for transfer buffer %i\n",
1066 cx231xx_uninit_isoc(dev
);
1069 memset(dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
1072 usb_rcvisocpipe(dev
->udev
, dev
->video_mode
.end_point_addr
);
1074 usb_fill_int_urb(urb
, dev
->udev
, pipe
,
1075 dev
->video_mode
.isoc_ctl
.transfer_buffer
[i
],
1076 sb_size
, cx231xx_isoc_irq_callback
, dma_q
, 1);
1078 urb
->number_of_packets
= max_packets
;
1079 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
1082 for (j
= 0; j
< max_packets
; j
++) {
1083 urb
->iso_frame_desc
[j
].offset
= k
;
1084 urb
->iso_frame_desc
[j
].length
=
1085 dev
->video_mode
.isoc_ctl
.max_pkt_size
;
1086 k
+= dev
->video_mode
.isoc_ctl
.max_pkt_size
;
1090 init_waitqueue_head(&dma_q
->wq
);
1092 /* submit urbs and enables IRQ */
1093 for (i
= 0; i
< dev
->video_mode
.isoc_ctl
.num_bufs
; i
++) {
1094 rc
= usb_submit_urb(dev
->video_mode
.isoc_ctl
.urb
[i
],
1098 "submit of urb %i failed (error=%i)\n", i
,
1100 cx231xx_uninit_isoc(dev
);
1105 if (dev
->mode_tv
== 0)
1106 cx231xx_capture_start(dev
, 1, Raw_Video
);
1108 cx231xx_capture_start(dev
, 1, TS1_serial_mode
);
1112 EXPORT_SYMBOL_GPL(cx231xx_init_isoc
);
1115 * Allocate URBs and start IRQ
1117 int cx231xx_init_bulk(struct cx231xx
*dev
, int max_packets
,
1118 int num_bufs
, int max_pkt_size
,
1119 int (*bulk_copy
) (struct cx231xx
*dev
, struct urb
*urb
))
1121 struct cx231xx_dmaqueue
*dma_q
= &dev
->video_mode
.vidq
;
1127 dev
->video_input
= dev
->video_input
> 2 ? 2 : dev
->video_input
;
1129 cx231xx_coredbg("Setting Video mux to %d\n", dev
->video_input
);
1131 video_mux(dev
, dev
->video_input
);
1133 /* De-allocates all pending stuff */
1134 cx231xx_uninit_bulk(dev
);
1136 dev
->video_mode
.bulk_ctl
.bulk_copy
= bulk_copy
;
1137 dev
->video_mode
.bulk_ctl
.num_bufs
= num_bufs
;
1139 dma_q
->is_partial_line
= 0;
1140 dma_q
->last_sav
= 0;
1141 dma_q
->current_field
= -1;
1142 dma_q
->field1_done
= 0;
1143 dma_q
->lines_per_field
= dev
->height
/ 2;
1144 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
1145 dma_q
->lines_completed
= 0;
1146 dma_q
->mpeg_buffer_done
= 0;
1147 dma_q
->left_data_count
= 0;
1148 dma_q
->mpeg_buffer_completed
= 0;
1149 dma_q
->ps_head
[0] = 0x00;
1150 dma_q
->ps_head
[1] = 0x00;
1151 dma_q
->ps_head
[2] = 0x01;
1152 dma_q
->ps_head
[3] = 0xBA;
1153 for (i
= 0; i
< 8; i
++)
1154 dma_q
->partial_buf
[i
] = 0;
1156 dev
->video_mode
.bulk_ctl
.urb
=
1157 kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
1158 if (!dev
->video_mode
.bulk_ctl
.urb
) {
1160 "cannot alloc memory for usb buffers\n");
1164 dev
->video_mode
.bulk_ctl
.transfer_buffer
=
1165 kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
1166 if (!dev
->video_mode
.bulk_ctl
.transfer_buffer
) {
1168 "cannot allocate memory for usbtransfer\n");
1169 kfree(dev
->video_mode
.bulk_ctl
.urb
);
1173 dev
->video_mode
.bulk_ctl
.max_pkt_size
= max_pkt_size
;
1174 dev
->video_mode
.bulk_ctl
.buf
= NULL
;
1176 sb_size
= max_packets
* dev
->video_mode
.bulk_ctl
.max_pkt_size
;
1178 if (dev
->mode_tv
== 1)
1179 dev
->video_mode
.end_point_addr
= 0x81;
1181 dev
->video_mode
.end_point_addr
= 0x84;
1184 /* allocate urbs and transfer buffers */
1185 for (i
= 0; i
< dev
->video_mode
.bulk_ctl
.num_bufs
; i
++) {
1186 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1188 cx231xx_uninit_bulk(dev
);
1191 dev
->video_mode
.bulk_ctl
.urb
[i
] = urb
;
1192 urb
->transfer_flags
= URB_NO_TRANSFER_DMA_MAP
;
1194 dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
] =
1195 usb_alloc_coherent(dev
->udev
, sb_size
, GFP_KERNEL
,
1196 &urb
->transfer_dma
);
1197 if (!dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
]) {
1199 "unable to allocate %i bytes for transfer buffer %i\n",
1201 cx231xx_uninit_bulk(dev
);
1204 memset(dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
], 0, sb_size
);
1206 pipe
= usb_rcvbulkpipe(dev
->udev
,
1207 dev
->video_mode
.end_point_addr
);
1208 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
1209 dev
->video_mode
.bulk_ctl
.transfer_buffer
[i
],
1210 sb_size
, cx231xx_bulk_irq_callback
, dma_q
);
1214 rc
= usb_clear_halt(dev
->udev
, dev
->video_mode
.bulk_ctl
.urb
[0]->pipe
);
1217 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1219 cx231xx_uninit_bulk(dev
);
1223 init_waitqueue_head(&dma_q
->wq
);
1225 /* submit urbs and enables IRQ */
1226 for (i
= 0; i
< dev
->video_mode
.bulk_ctl
.num_bufs
; i
++) {
1227 rc
= usb_submit_urb(dev
->video_mode
.bulk_ctl
.urb
[i
],
1231 "submit of urb %i failed (error=%i)\n", i
, rc
);
1232 cx231xx_uninit_bulk(dev
);
1237 if (dev
->mode_tv
== 0)
1238 cx231xx_capture_start(dev
, 1, Raw_Video
);
1240 cx231xx_capture_start(dev
, 1, TS1_serial_mode
);
1244 EXPORT_SYMBOL_GPL(cx231xx_init_bulk
);
1245 void cx231xx_stop_TS1(struct cx231xx
*dev
)
1247 u8 val
[4] = { 0, 0, 0, 0 };
1253 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
1254 TS_MODE_REG
, val
, 4);
1260 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
1261 TS1_CFG_REG
, val
, 4);
1263 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
1264 void cx231xx_start_TS1(struct cx231xx
*dev
)
1266 u8 val
[4] = { 0, 0, 0, 0 };
1272 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
1273 TS_MODE_REG
, val
, 4);
1279 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
,
1280 TS1_CFG_REG
, val
, 4);
1282 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
1283 /*****************************************************************
1284 * Device Init/UnInit functions *
1285 ******************************************************************/
1286 int cx231xx_dev_init(struct cx231xx
*dev
)
1290 /* Initialize I2C bus */
1292 /* External Master 1 Bus */
1293 dev
->i2c_bus
[0].nr
= 0;
1294 dev
->i2c_bus
[0].dev
= dev
;
1295 dev
->i2c_bus
[0].i2c_period
= I2C_SPEED_100K
; /* 100 KHz */
1296 dev
->i2c_bus
[0].i2c_nostop
= 0;
1297 dev
->i2c_bus
[0].i2c_reserve
= 0;
1298 dev
->i2c_bus
[0].i2c_rc
= -ENODEV
;
1300 /* External Master 2 Bus */
1301 dev
->i2c_bus
[1].nr
= 1;
1302 dev
->i2c_bus
[1].dev
= dev
;
1303 dev
->i2c_bus
[1].i2c_period
= I2C_SPEED_100K
; /* 100 KHz */
1304 dev
->i2c_bus
[1].i2c_nostop
= 0;
1305 dev
->i2c_bus
[1].i2c_reserve
= 0;
1306 dev
->i2c_bus
[1].i2c_rc
= -ENODEV
;
1308 /* Internal Master 3 Bus */
1309 dev
->i2c_bus
[2].nr
= 2;
1310 dev
->i2c_bus
[2].dev
= dev
;
1311 dev
->i2c_bus
[2].i2c_period
= I2C_SPEED_100K
; /* 100kHz */
1312 dev
->i2c_bus
[2].i2c_nostop
= 0;
1313 dev
->i2c_bus
[2].i2c_reserve
= 0;
1314 dev
->i2c_bus
[2].i2c_rc
= -ENODEV
;
1316 /* register I2C buses */
1317 errCode
= cx231xx_i2c_register(&dev
->i2c_bus
[0]);
1320 errCode
= cx231xx_i2c_register(&dev
->i2c_bus
[1]);
1323 errCode
= cx231xx_i2c_register(&dev
->i2c_bus
[2]);
1327 errCode
= cx231xx_i2c_mux_create(dev
);
1330 "%s: Failed to create I2C mux\n", __func__
);
1333 errCode
= cx231xx_i2c_mux_register(dev
, 0);
1337 errCode
= cx231xx_i2c_mux_register(dev
, 1);
1341 /* scan the real bus segments in the order of physical port numbers */
1342 cx231xx_do_i2c_scan(dev
, I2C_0
);
1343 cx231xx_do_i2c_scan(dev
, I2C_1_MUX_1
);
1344 cx231xx_do_i2c_scan(dev
, I2C_2
);
1345 cx231xx_do_i2c_scan(dev
, I2C_1_MUX_3
);
1348 /* Note : with out calling set power mode function,
1349 afe can not be set up correctly */
1350 if (dev
->board
.external_av
) {
1351 errCode
= cx231xx_set_power_mode(dev
,
1352 POLARIS_AVMODE_ENXTERNAL_AV
);
1355 "%s: Failed to set Power - errCode [%d]!\n",
1360 errCode
= cx231xx_set_power_mode(dev
,
1361 POLARIS_AVMODE_ANALOGT_TV
);
1364 "%s: Failed to set Power - errCode [%d]!\n",
1370 /* reset the Tuner, if it is a Xceive tuner */
1371 if ((dev
->board
.tuner_type
== TUNER_XC5000
) ||
1372 (dev
->board
.tuner_type
== TUNER_XC2028
))
1373 cx231xx_gpio_set(dev
, dev
->board
.tuner_gpio
);
1375 /* initialize Colibri block */
1376 errCode
= cx231xx_afe_init_super_block(dev
, 0x23c);
1379 "%s: cx231xx_afe init super block - errCode [%d]!\n",
1383 errCode
= cx231xx_afe_init_channels(dev
);
1386 "%s: cx231xx_afe init channels - errCode [%d]!\n",
1391 /* Set DIF in By pass mode */
1392 errCode
= cx231xx_dif_set_standard(dev
, DIF_USE_BASEBAND
);
1395 "%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
1400 /* I2S block related functions */
1401 errCode
= cx231xx_i2s_blk_initialize(dev
);
1404 "%s: cx231xx_i2s block initialize - errCode [%d]!\n",
1409 /* init control pins */
1410 errCode
= cx231xx_init_ctrl_pin_status(dev
);
1413 "%s: cx231xx_init ctrl pins - errCode [%d]!\n",
1418 /* set AGC mode to Analog */
1419 switch (dev
->model
) {
1420 case CX231XX_BOARD_CNXT_CARRAERA
:
1421 case CX231XX_BOARD_CNXT_RDE_250
:
1422 case CX231XX_BOARD_CNXT_SHELBY
:
1423 case CX231XX_BOARD_CNXT_RDU_250
:
1424 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 1);
1426 case CX231XX_BOARD_CNXT_RDE_253S
:
1427 case CX231XX_BOARD_CNXT_RDU_253S
:
1428 case CX231XX_BOARD_HAUPPAUGE_EXETER
:
1429 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx
:
1430 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID
:
1431 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL
:
1432 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC
:
1433 errCode
= cx231xx_set_agc_analog_digital_mux_select(dev
, 0);
1440 "%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
1445 /* set all alternate settings to zero initially */
1446 cx231xx_set_alt_setting(dev
, INDEX_VIDEO
, 0);
1447 cx231xx_set_alt_setting(dev
, INDEX_VANC
, 0);
1448 cx231xx_set_alt_setting(dev
, INDEX_HANC
, 0);
1449 if (dev
->board
.has_dvb
)
1450 cx231xx_set_alt_setting(dev
, INDEX_TS1
, 0);
1455 EXPORT_SYMBOL_GPL(cx231xx_dev_init
);
1457 void cx231xx_dev_uninit(struct cx231xx
*dev
)
1459 /* Un Initialize I2C bus */
1460 cx231xx_i2c_mux_unregister(dev
);
1461 cx231xx_i2c_unregister(&dev
->i2c_bus
[2]);
1462 cx231xx_i2c_unregister(&dev
->i2c_bus
[1]);
1463 cx231xx_i2c_unregister(&dev
->i2c_bus
[0]);
1465 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit
);
1467 /*****************************************************************
1468 * G P I O related functions *
1469 ******************************************************************/
1470 int cx231xx_send_gpio_cmd(struct cx231xx
*dev
, u32 gpio_bit
, u8
*gpio_val
,
1471 u8 len
, u8 request
, u8 direction
)
1474 struct VENDOR_REQUEST_IN ven_req
;
1477 ven_req
.wValue
= (u16
) (gpio_bit
>> 16 & 0xffff);
1482 ven_req
.bRequest
= VRT_GET_GPIO
; /* 0x9 gpio */
1484 ven_req
.bRequest
= VRT_SET_GPIO
; /* 0x8 gpio */
1487 ven_req
.bRequest
= VRT_GET_GPIE
; /* 0xb gpie */
1489 ven_req
.bRequest
= VRT_SET_GPIE
; /* 0xa gpie */
1492 /* set index value */
1493 ven_req
.wIndex
= (u16
) (gpio_bit
& 0xffff);
1495 /* set wLength value */
1496 ven_req
.wLength
= len
;
1498 /* set bData value */
1501 /* set the buffer for read / write */
1502 ven_req
.pBuff
= gpio_val
;
1504 /* set the direction */
1506 ven_req
.direction
= USB_DIR_IN
;
1507 memset(ven_req
.pBuff
, 0x00, ven_req
.wLength
);
1509 ven_req
.direction
= USB_DIR_OUT
;
1512 /* call common vendor command request */
1513 status
= cx231xx_send_vendor_cmd(dev
, &ven_req
);
1515 dev_err(dev
->dev
, "%s: failed with status -%d\n",
1521 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd
);
1523 /*****************************************************************
1524 * C O N T R O L - Register R E A D / W R I T E functions *
1525 *****************************************************************/
1526 int cx231xx_mode_register(struct cx231xx
*dev
, u16 address
, u32 mode
)
1528 u8 value
[4] = { 0x0, 0x0, 0x0, 0x0 };
1533 cx231xx_read_ctrl_reg(dev
, VRT_GET_REGISTER
, address
, value
, 4);
1537 tmp
= le32_to_cpu(*((__le32
*) value
));
1540 value
[0] = (u8
) tmp
;
1541 value
[1] = (u8
) (tmp
>> 8);
1542 value
[2] = (u8
) (tmp
>> 16);
1543 value
[3] = (u8
) (tmp
>> 24);
1546 cx231xx_write_ctrl_reg(dev
, VRT_SET_REGISTER
, address
, value
, 4);
1551 /*****************************************************************
1552 * I 2 C Internal C O N T R O L functions *
1553 *****************************************************************/
1554 int cx231xx_read_i2c_master(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1555 u8 saddr_len
, u32
*data
, u8 data_len
, int master
)
1558 struct cx231xx_i2c_xfer_data req_data
;
1563 else if (saddr_len
== 1)
1566 /* prepare xfer_data struct */
1567 req_data
.dev_addr
= dev_addr
>> 1;
1568 req_data
.direction
= I2C_M_RD
;
1569 req_data
.saddr_len
= saddr_len
;
1570 req_data
.saddr_dat
= saddr
;
1571 req_data
.buf_size
= data_len
;
1572 req_data
.p_buffer
= (u8
*) value
;
1574 /* usb send command */
1576 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0],
1578 else if (master
== 1)
1579 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[1],
1581 else if (master
== 2)
1582 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[2],
1586 /* Copy the data read back to main buffer */
1589 else if (data_len
== 4)
1591 value
[0] | value
[1] << 8 | value
[2] << 16 | value
[3]
1593 else if (data_len
> 4)
1594 *data
= value
[saddr
];
1600 int cx231xx_write_i2c_master(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1601 u8 saddr_len
, u32 data
, u8 data_len
, int master
)
1604 u8 value
[4] = { 0, 0, 0, 0 };
1605 struct cx231xx_i2c_xfer_data req_data
;
1607 value
[0] = (u8
) data
;
1608 value
[1] = (u8
) (data
>> 8);
1609 value
[2] = (u8
) (data
>> 16);
1610 value
[3] = (u8
) (data
>> 24);
1614 else if (saddr_len
== 1)
1617 /* prepare xfer_data struct */
1618 req_data
.dev_addr
= dev_addr
>> 1;
1619 req_data
.direction
= 0;
1620 req_data
.saddr_len
= saddr_len
;
1621 req_data
.saddr_dat
= saddr
;
1622 req_data
.buf_size
= data_len
;
1623 req_data
.p_buffer
= value
;
1625 /* usb send command */
1627 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0],
1629 else if (master
== 1)
1630 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[1],
1632 else if (master
== 2)
1633 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[2],
1639 int cx231xx_read_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1640 u8 saddr_len
, u32
*data
, u8 data_len
)
1643 struct cx231xx_i2c_xfer_data req_data
;
1644 u8 value
[4] = { 0, 0, 0, 0 };
1648 else if (saddr_len
== 1)
1651 /* prepare xfer_data struct */
1652 req_data
.dev_addr
= dev_addr
>> 1;
1653 req_data
.direction
= I2C_M_RD
;
1654 req_data
.saddr_len
= saddr_len
;
1655 req_data
.saddr_dat
= saddr
;
1656 req_data
.buf_size
= data_len
;
1657 req_data
.p_buffer
= (u8
*) value
;
1659 /* usb send command */
1660 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1663 /* Copy the data read back to main buffer */
1668 value
[0] | value
[1] << 8 | value
[2] << 16 | value
[3]
1675 int cx231xx_write_i2c_data(struct cx231xx
*dev
, u8 dev_addr
, u16 saddr
,
1676 u8 saddr_len
, u32 data
, u8 data_len
)
1679 u8 value
[4] = { 0, 0, 0, 0 };
1680 struct cx231xx_i2c_xfer_data req_data
;
1682 value
[0] = (u8
) data
;
1683 value
[1] = (u8
) (data
>> 8);
1684 value
[2] = (u8
) (data
>> 16);
1685 value
[3] = (u8
) (data
>> 24);
1689 else if (saddr_len
== 1)
1692 /* prepare xfer_data struct */
1693 req_data
.dev_addr
= dev_addr
>> 1;
1694 req_data
.direction
= 0;
1695 req_data
.saddr_len
= saddr_len
;
1696 req_data
.saddr_dat
= saddr
;
1697 req_data
.buf_size
= data_len
;
1698 req_data
.p_buffer
= value
;
1700 /* usb send command */
1701 status
= dev
->cx231xx_send_usb_command(&dev
->i2c_bus
[0], &req_data
);
1706 int cx231xx_reg_mask_write(struct cx231xx
*dev
, u8 dev_addr
, u8 size
,
1707 u16 register_address
, u8 bit_start
, u8 bit_end
,
1715 if (bit_start
> (size
- 1) || bit_end
> (size
- 1))
1720 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1724 cx231xx_read_i2c_data(dev
, dev_addr
, register_address
, 2,
1731 mask
= 1 << bit_end
;
1732 for (i
= bit_end
; i
> bit_start
&& i
> 0; i
--)
1733 mask
= mask
+ (1 << (i
- 1));
1735 value
<<= bit_start
;
1742 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1748 cx231xx_write_i2c_data(dev
, dev_addr
, register_address
, 2,
1755 int cx231xx_read_modify_write_i2c_dword(struct cx231xx
*dev
, u8 dev_addr
,
1756 u16 saddr
, u32 mask
, u32 value
)
1761 status
= cx231xx_read_i2c_data(dev
, dev_addr
, saddr
, 2, &temp
, 4);
1769 status
= cx231xx_write_i2c_data(dev
, dev_addr
, saddr
, 2, temp
, 4);
1774 u32
cx231xx_set_field(u32 field_mask
, u32 data
)
1778 for (temp
= field_mask
; (temp
& 1) == 0; temp
>>= 1)