sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / usb / cx231xx / cx231xx-core.c
blob550ec932f931950b28e072dd7b918bf33223bf1e
1 /*
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>
6 Based on em28xx driver
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 "cx231xx.h"
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <media/v4l2-common.h>
30 #include <media/tuner.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 {\
41 if (core_debug) \
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 static int alt = CX231XX_PINOUT;
50 module_param(alt, int, 0644);
51 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
53 #define cx231xx_isocdbg(fmt, arg...) do {\
54 if (core_debug) \
55 printk(KERN_INFO "%s %s :"fmt, \
56 dev->name, __func__ , ##arg); } while (0)
58 /*****************************************************************
59 * Device control list functions *
60 ******************************************************************/
62 LIST_HEAD(cx231xx_devlist);
63 static DEFINE_MUTEX(cx231xx_devlist_mutex);
66 * cx231xx_realease_resources()
67 * unregisters the v4l2,i2c and usb devices
68 * called when the device gets disconected or at module unload
70 void cx231xx_remove_from_devlist(struct cx231xx *dev)
72 if (dev == NULL)
73 return;
74 if (dev->udev == NULL)
75 return;
77 if (atomic_read(&dev->devlist_count) > 0) {
78 mutex_lock(&cx231xx_devlist_mutex);
79 list_del(&dev->devlist);
80 atomic_dec(&dev->devlist_count);
81 mutex_unlock(&cx231xx_devlist_mutex);
85 void cx231xx_add_into_devlist(struct cx231xx *dev)
87 mutex_lock(&cx231xx_devlist_mutex);
88 list_add_tail(&dev->devlist, &cx231xx_devlist);
89 atomic_inc(&dev->devlist_count);
90 mutex_unlock(&cx231xx_devlist_mutex);
93 static LIST_HEAD(cx231xx_extension_devlist);
95 int cx231xx_register_extension(struct cx231xx_ops *ops)
97 struct cx231xx *dev = NULL;
99 mutex_lock(&cx231xx_devlist_mutex);
100 list_add_tail(&ops->next, &cx231xx_extension_devlist);
101 list_for_each_entry(dev, &cx231xx_devlist, devlist) {
102 ops->init(dev);
103 dev_info(dev->dev, "%s initialized\n", ops->name);
105 mutex_unlock(&cx231xx_devlist_mutex);
106 return 0;
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) {
116 ops->fini(dev);
117 dev_info(dev->dev, "%s removed\n", ops->name);
120 list_del(&ops->next);
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_devlist_mutex);
130 if (!list_empty(&cx231xx_extension_devlist)) {
131 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
132 if (ops->init)
133 ops->init(dev);
136 mutex_unlock(&cx231xx_devlist_mutex);
139 void cx231xx_close_extension(struct cx231xx *dev)
141 struct cx231xx_ops *ops = NULL;
143 mutex_lock(&cx231xx_devlist_mutex);
144 if (!list_empty(&cx231xx_extension_devlist)) {
145 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
146 if (ops->fini)
147 ops->fini(dev);
150 mutex_unlock(&cx231xx_devlist_mutex);
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)
159 int status = 0;
160 struct cx231xx *dev = i2c_bus->dev;
161 struct VENDOR_REQUEST_IN ven_req;
163 u8 saddr_len = 0;
164 u8 _i2c_period = 0;
165 u8 _i2c_nostop = 0;
166 u8 _i2c_reserve = 0;
168 if (dev->state & DEV_DISCONNECTED)
169 return -ENODEV;
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;
178 /* Set wValue */
179 ven_req.wValue = (req_data->dev_addr << 9 | _i2c_period << 4 |
180 saddr_len << 2 | _i2c_nostop << 1 | I2C_SYNC |
181 _i2c_reserve << 6);
183 /* set channel number */
184 if (req_data->direction & I2C_M_RD) {
185 /* channel number, for read,spec required channel_num +4 */
186 ven_req.bRequest = i2c_bus->nr + 4;
187 } else
188 ven_req.bRequest = i2c_bus->nr; /* channel number, */
190 /* set index value */
191 switch (saddr_len) {
192 case 0:
193 ven_req.wIndex = 0; /* need check */
194 break;
195 case 1:
196 ven_req.wIndex = (req_data->saddr_dat & 0xff);
197 break;
198 case 2:
199 ven_req.wIndex = req_data->saddr_dat;
200 break;
203 /* set wLength value */
204 ven_req.wLength = req_data->buf_size;
206 /* set bData value */
207 ven_req.bData = 0;
209 /* set the direction */
210 if (req_data->direction) {
211 ven_req.direction = USB_DIR_IN;
212 memset(req_data->p_buffer, 0x00, ven_req.wLength);
213 } else
214 ven_req.direction = USB_DIR_OUT;
216 /* set the buffer for read / write */
217 ven_req.pBuff = req_data->p_buffer;
220 /* call common vendor command request */
221 status = cx231xx_send_vendor_cmd(dev, &ven_req);
222 if (status < 0 && !dev->i2c_scan_running) {
223 dev_err(dev->dev, "%s: failed with status -%d\n",
224 __func__, status);
227 return status;
229 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
232 * Sends/Receives URB control messages, assuring to use a kalloced buffer
233 * for all operations (dev->urb_buf), to avoid using stacked buffers, as
234 * they aren't safe for usage with USB, due to DMA restrictions.
235 * Also implements the debug code for control URB's.
237 static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
238 __u8 request, __u8 requesttype, __u16 value, __u16 index,
239 void *data, __u16 size, int timeout)
241 int rc, i;
243 if (reg_debug) {
244 printk(KERN_DEBUG "%s: (pipe 0x%08x): %s: %02x %02x %02x %02x %02x %02x %02x %02x ",
245 dev->name,
246 pipe,
247 (requesttype & USB_DIR_IN) ? "IN" : "OUT",
248 requesttype,
249 request,
250 value & 0xff, value >> 8,
251 index & 0xff, index >> 8,
252 size & 0xff, size >> 8);
253 if (!(requesttype & USB_DIR_IN)) {
254 printk(KERN_CONT ">>>");
255 for (i = 0; i < size; i++)
256 printk(KERN_CONT " %02x",
257 ((unsigned char *)data)[i]);
261 /* Do the real call to usb_control_msg */
262 mutex_lock(&dev->ctrl_urb_lock);
263 if (!(requesttype & USB_DIR_IN) && size)
264 memcpy(dev->urb_buf, data, size);
265 rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
266 index, dev->urb_buf, size, timeout);
267 if ((requesttype & USB_DIR_IN) && size)
268 memcpy(data, dev->urb_buf, size);
269 mutex_unlock(&dev->ctrl_urb_lock);
271 if (reg_debug) {
272 if (unlikely(rc < 0)) {
273 printk(KERN_CONT "FAILED!\n");
274 return rc;
277 if ((requesttype & USB_DIR_IN)) {
278 printk(KERN_CONT "<<<");
279 for (i = 0; i < size; i++)
280 printk(KERN_CONT " %02x",
281 ((unsigned char *)data)[i]);
283 printk(KERN_CONT "\n");
286 return rc;
291 * cx231xx_read_ctrl_reg()
292 * reads data from the usb device specifying bRequest and wValue
294 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
295 char *buf, int len)
297 u8 val = 0;
298 int ret;
299 int pipe = usb_rcvctrlpipe(dev->udev, 0);
301 if (dev->state & DEV_DISCONNECTED)
302 return -ENODEV;
304 if (len > URB_MAX_CTRL_SIZE)
305 return -EINVAL;
307 switch (len) {
308 case 1:
309 val = ENABLE_ONE_BYTE;
310 break;
311 case 2:
312 val = ENABLE_TWE_BYTE;
313 break;
314 case 3:
315 val = ENABLE_THREE_BYTE;
316 break;
317 case 4:
318 val = ENABLE_FOUR_BYTE;
319 break;
320 default:
321 val = 0xFF; /* invalid option */
324 if (val == 0xFF)
325 return -EINVAL;
327 ret = __usb_control_msg(dev, pipe, req,
328 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
329 val, reg, buf, len, HZ);
330 return ret;
333 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
334 struct VENDOR_REQUEST_IN *ven_req)
336 int ret;
337 int pipe = 0;
338 int unsend_size = 0;
339 u8 *pdata;
341 if (dev->state & DEV_DISCONNECTED)
342 return -ENODEV;
344 if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
345 return -EINVAL;
347 if (ven_req->direction)
348 pipe = usb_rcvctrlpipe(dev->udev, 0);
349 else
350 pipe = usb_sndctrlpipe(dev->udev, 0);
353 * If the cx23102 read more than 4 bytes with i2c bus,
354 * need chop to 4 byte per request
356 if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
357 (ven_req->bRequest == 0x5) ||
358 (ven_req->bRequest == 0x6))) {
359 unsend_size = 0;
360 pdata = ven_req->pBuff;
363 unsend_size = ven_req->wLength;
365 /* the first package */
366 ven_req->wValue = ven_req->wValue & 0xFFFB;
367 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
368 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
369 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
370 ven_req->wValue, ven_req->wIndex, pdata,
371 0x0004, HZ);
372 unsend_size = unsend_size - 4;
374 /* the middle package */
375 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
376 while (unsend_size - 4 > 0) {
377 pdata = pdata + 4;
378 ret = __usb_control_msg(dev, pipe,
379 ven_req->bRequest,
380 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
381 ven_req->wValue, ven_req->wIndex, pdata,
382 0x0004, HZ);
383 unsend_size = unsend_size - 4;
386 /* the last package */
387 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
388 pdata = pdata + 4;
389 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
390 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
391 ven_req->wValue, ven_req->wIndex, pdata,
392 unsend_size, HZ);
393 } else {
394 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
395 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
396 ven_req->wValue, ven_req->wIndex,
397 ven_req->pBuff, ven_req->wLength, HZ);
400 return ret;
404 * cx231xx_write_ctrl_reg()
405 * sends data to the usb device, specifying bRequest
407 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
408 int len)
410 u8 val = 0;
411 int ret;
412 int pipe = usb_sndctrlpipe(dev->udev, 0);
414 if (dev->state & DEV_DISCONNECTED)
415 return -ENODEV;
417 if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
418 return -EINVAL;
420 switch (len) {
421 case 1:
422 val = ENABLE_ONE_BYTE;
423 break;
424 case 2:
425 val = ENABLE_TWE_BYTE;
426 break;
427 case 3:
428 val = ENABLE_THREE_BYTE;
429 break;
430 case 4:
431 val = ENABLE_FOUR_BYTE;
432 break;
433 default:
434 val = 0xFF; /* invalid option */
437 if (val == 0xFF)
438 return -EINVAL;
440 if (reg_debug) {
441 int byte;
443 cx231xx_isocdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
444 pipe,
445 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
446 req, 0, val, reg & 0xff,
447 reg >> 8, len & 0xff, len >> 8);
449 for (byte = 0; byte < len; byte++)
450 cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
451 cx231xx_isocdbg("\n");
454 ret = __usb_control_msg(dev, pipe, req,
455 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
456 val, reg, buf, len, HZ);
458 return ret;
461 /****************************************************************
462 * USB Alternate Setting functions *
463 *****************************************************************/
465 int cx231xx_set_video_alternate(struct cx231xx *dev)
467 int errCode, prev_alt = dev->video_mode.alt;
468 unsigned int min_pkt_size = dev->width * 2 + 4;
469 u32 usb_interface_index = 0;
471 /* When image size is bigger than a certain value,
472 the frame size should be increased, otherwise, only
473 green screen will be received.
475 if (dev->width * 2 * dev->height > 720 * 240 * 2)
476 min_pkt_size *= 2;
478 if (dev->width > 360) {
479 /* resolutions: 720,704,640 */
480 dev->video_mode.alt = 3;
481 } else if (dev->width > 180) {
482 /* resolutions: 360,352,320,240 */
483 dev->video_mode.alt = 2;
484 } else if (dev->width > 0) {
485 /* resolutions: 180,176,160,128,88 */
486 dev->video_mode.alt = 1;
487 } else {
488 /* Change to alt0 BULK to release USB bandwidth */
489 dev->video_mode.alt = 0;
492 if (dev->USE_ISO == 0)
493 dev->video_mode.alt = 0;
495 cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
497 /* Get the correct video interface Index */
498 usb_interface_index =
499 dev->current_pcb_config.hs_config_info[0].interface_info.
500 video_index + 1;
502 if (dev->video_mode.alt != prev_alt) {
503 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
504 min_pkt_size, dev->video_mode.alt);
506 if (dev->video_mode.alt_max_pkt_size != NULL)
507 dev->video_mode.max_pkt_size =
508 dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
509 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
510 dev->video_mode.alt,
511 dev->video_mode.max_pkt_size);
512 errCode =
513 usb_set_interface(dev->udev, usb_interface_index,
514 dev->video_mode.alt);
515 if (errCode < 0) {
516 dev_err(dev->dev,
517 "cannot change alt number to %d (error=%i)\n",
518 dev->video_mode.alt, errCode);
519 return errCode;
522 return 0;
525 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
527 int status = 0;
528 u32 usb_interface_index = 0;
529 u32 max_pkt_size = 0;
531 switch (index) {
532 case INDEX_TS1:
533 usb_interface_index =
534 dev->current_pcb_config.hs_config_info[0].interface_info.
535 ts1_index + 1;
536 dev->ts1_mode.alt = alt;
537 if (dev->ts1_mode.alt_max_pkt_size != NULL)
538 max_pkt_size = dev->ts1_mode.max_pkt_size =
539 dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
540 break;
541 case INDEX_TS2:
542 usb_interface_index =
543 dev->current_pcb_config.hs_config_info[0].interface_info.
544 ts2_index + 1;
545 break;
546 case INDEX_AUDIO:
547 usb_interface_index =
548 dev->current_pcb_config.hs_config_info[0].interface_info.
549 audio_index + 1;
550 dev->adev.alt = alt;
551 if (dev->adev.alt_max_pkt_size != NULL)
552 max_pkt_size = dev->adev.max_pkt_size =
553 dev->adev.alt_max_pkt_size[dev->adev.alt];
554 break;
555 case INDEX_VIDEO:
556 usb_interface_index =
557 dev->current_pcb_config.hs_config_info[0].interface_info.
558 video_index + 1;
559 dev->video_mode.alt = alt;
560 if (dev->video_mode.alt_max_pkt_size != NULL)
561 max_pkt_size = dev->video_mode.max_pkt_size =
562 dev->video_mode.alt_max_pkt_size[dev->video_mode.
563 alt];
564 break;
565 case INDEX_VANC:
566 if (dev->board.no_alt_vanc)
567 return 0;
568 usb_interface_index =
569 dev->current_pcb_config.hs_config_info[0].interface_info.
570 vanc_index + 1;
571 dev->vbi_mode.alt = alt;
572 if (dev->vbi_mode.alt_max_pkt_size != NULL)
573 max_pkt_size = dev->vbi_mode.max_pkt_size =
574 dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
575 break;
576 case INDEX_HANC:
577 usb_interface_index =
578 dev->current_pcb_config.hs_config_info[0].interface_info.
579 hanc_index + 1;
580 dev->sliced_cc_mode.alt = alt;
581 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
582 max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
583 dev->sliced_cc_mode.alt_max_pkt_size[dev->
584 sliced_cc_mode.
585 alt];
586 break;
587 default:
588 break;
591 if (alt > 0 && max_pkt_size == 0) {
592 dev_err(dev->dev,
593 "can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
594 usb_interface_index, alt);
595 /*To workaround error number=-71 on EP0 for videograbber,
596 need add following codes.*/
597 if (dev->board.no_alt_vanc)
598 return -1;
601 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,Interface = %d\n",
602 alt, max_pkt_size,
603 usb_interface_index);
605 if (usb_interface_index > 0) {
606 status = usb_set_interface(dev->udev, usb_interface_index, alt);
607 if (status < 0) {
608 dev_err(dev->dev,
609 "can't change interface %d alt no. to %d (err=%i)\n",
610 usb_interface_index, alt, status);
611 return status;
615 return status;
617 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
619 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
621 int rc = 0;
623 if (!gpio)
624 return rc;
626 /* Send GPIO reset sequences specified at board entry */
627 while (gpio->sleep >= 0) {
628 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
629 if (rc < 0)
630 return rc;
632 if (gpio->sleep > 0)
633 msleep(gpio->sleep);
635 gpio++;
637 return rc;
640 int cx231xx_demod_reset(struct cx231xx *dev)
643 u8 status = 0;
644 u8 value[4] = { 0, 0, 0, 0 };
646 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
647 value, 4);
649 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
650 value[0], value[1], value[2], value[3]);
652 cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
654 value[1] = (u8) 0x3;
655 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
656 PWR_CTL_EN, value, 4);
657 msleep(10);
659 value[1] = (u8) 0x0;
660 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
661 PWR_CTL_EN, value, 4);
662 msleep(10);
664 value[1] = (u8) 0x3;
665 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
666 PWR_CTL_EN, value, 4);
667 msleep(10);
669 status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
670 value, 4);
672 cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
673 value[0], value[1], value[2], value[3]);
675 return status;
677 EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
678 int is_fw_load(struct cx231xx *dev)
680 return cx231xx_check_fw(dev);
682 EXPORT_SYMBOL_GPL(is_fw_load);
684 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
686 int errCode = 0;
688 if (dev->mode == set_mode)
689 return 0;
691 if (set_mode == CX231XX_SUSPEND) {
692 /* Set the chip in power saving mode */
693 dev->mode = set_mode;
696 /* Resource is locked */
697 if (dev->mode != CX231XX_SUSPEND)
698 return -EINVAL;
700 dev->mode = set_mode;
702 if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
703 /* set AGC mode to Digital */
704 switch (dev->model) {
705 case CX231XX_BOARD_CNXT_CARRAERA:
706 case CX231XX_BOARD_CNXT_RDE_250:
707 case CX231XX_BOARD_CNXT_SHELBY:
708 case CX231XX_BOARD_CNXT_RDU_250:
709 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
710 break;
711 case CX231XX_BOARD_CNXT_RDE_253S:
712 case CX231XX_BOARD_CNXT_RDU_253S:
713 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
714 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
715 break;
716 case CX231XX_BOARD_HAUPPAUGE_EXETER:
717 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
718 errCode = cx231xx_set_power_mode(dev,
719 POLARIS_AVMODE_DIGITAL);
720 break;
721 default:
722 break;
724 } else/* Set Analog Power mode */ {
725 /* set AGC mode to Analog */
726 switch (dev->model) {
727 case CX231XX_BOARD_CNXT_CARRAERA:
728 case CX231XX_BOARD_CNXT_RDE_250:
729 case CX231XX_BOARD_CNXT_SHELBY:
730 case CX231XX_BOARD_CNXT_RDU_250:
731 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
732 break;
733 case CX231XX_BOARD_CNXT_RDE_253S:
734 case CX231XX_BOARD_CNXT_RDU_253S:
735 case CX231XX_BOARD_HAUPPAUGE_EXETER:
736 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
737 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
738 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
739 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
740 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
741 break;
742 default:
743 break;
747 if (errCode < 0) {
748 dev_err(dev->dev, "Failed to set devmode to %s: error: %i",
749 dev->mode == CX231XX_DIGITAL_MODE ? "digital" : "analog",
750 errCode);
751 return errCode;
754 return 0;
756 EXPORT_SYMBOL_GPL(cx231xx_set_mode);
758 int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
760 int errCode = 0;
761 int actlen = -1;
762 int ret = -ENOMEM;
763 u32 *buffer;
765 buffer = kzalloc(4096, GFP_KERNEL);
766 if (buffer == NULL)
767 return -ENOMEM;
768 memcpy(&buffer[0], firmware, 4096);
770 ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
771 buffer, 4096, &actlen, 2000);
773 if (ret)
774 dev_err(dev->dev,
775 "bulk message failed: %d (%d/%d)", ret,
776 size, actlen);
777 else {
778 errCode = actlen != size ? -1 : 0;
780 kfree(buffer);
781 return errCode;
784 /*****************************************************************
785 * URB Streaming functions *
786 ******************************************************************/
789 * IRQ callback, called by URB callback
791 static void cx231xx_isoc_irq_callback(struct urb *urb)
793 struct cx231xx_dmaqueue *dma_q = urb->context;
794 struct cx231xx_video_mode *vmode =
795 container_of(dma_q, struct cx231xx_video_mode, vidq);
796 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
797 int i;
799 switch (urb->status) {
800 case 0: /* success */
801 case -ETIMEDOUT: /* NAK */
802 break;
803 case -ECONNRESET: /* kill */
804 case -ENOENT:
805 case -ESHUTDOWN:
806 return;
807 default: /* error */
808 cx231xx_isocdbg("urb completion error %d.\n", urb->status);
809 break;
812 /* Copy data from URB */
813 spin_lock(&dev->video_mode.slock);
814 dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
815 spin_unlock(&dev->video_mode.slock);
817 /* Reset urb buffers */
818 for (i = 0; i < urb->number_of_packets; i++) {
819 urb->iso_frame_desc[i].status = 0;
820 urb->iso_frame_desc[i].actual_length = 0;
823 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
824 if (urb->status) {
825 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
826 urb->status);
829 /*****************************************************************
830 * URB Streaming functions *
831 ******************************************************************/
834 * IRQ callback, called by URB callback
836 static void cx231xx_bulk_irq_callback(struct urb *urb)
838 struct cx231xx_dmaqueue *dma_q = urb->context;
839 struct cx231xx_video_mode *vmode =
840 container_of(dma_q, struct cx231xx_video_mode, vidq);
841 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
843 switch (urb->status) {
844 case 0: /* success */
845 case -ETIMEDOUT: /* NAK */
846 break;
847 case -ECONNRESET: /* kill */
848 case -ENOENT:
849 case -ESHUTDOWN:
850 return;
851 case -EPIPE: /* stall */
852 cx231xx_isocdbg("urb completion error - device is stalled.\n");
853 return;
854 default: /* error */
855 cx231xx_isocdbg("urb completion error %d.\n", urb->status);
856 break;
859 /* Copy data from URB */
860 spin_lock(&dev->video_mode.slock);
861 dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
862 spin_unlock(&dev->video_mode.slock);
864 /* Reset urb buffers */
865 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
866 if (urb->status) {
867 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
868 urb->status);
872 * Stop and Deallocate URBs
874 void cx231xx_uninit_isoc(struct cx231xx *dev)
876 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
877 struct urb *urb;
878 int i;
879 bool broken_pipe = false;
881 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
883 dev->video_mode.isoc_ctl.nfields = -1;
884 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
885 urb = dev->video_mode.isoc_ctl.urb[i];
886 if (urb) {
887 if (!irqs_disabled())
888 usb_kill_urb(urb);
889 else
890 usb_unlink_urb(urb);
892 if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
893 usb_free_coherent(dev->udev,
894 urb->transfer_buffer_length,
895 dev->video_mode.isoc_ctl.
896 transfer_buffer[i],
897 urb->transfer_dma);
899 if (urb->status == -EPIPE) {
900 broken_pipe = true;
902 usb_free_urb(urb);
903 dev->video_mode.isoc_ctl.urb[i] = NULL;
905 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
908 if (broken_pipe) {
909 cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
910 usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
912 kfree(dev->video_mode.isoc_ctl.urb);
913 kfree(dev->video_mode.isoc_ctl.transfer_buffer);
914 kfree(dma_q->p_left_data);
916 dev->video_mode.isoc_ctl.urb = NULL;
917 dev->video_mode.isoc_ctl.transfer_buffer = NULL;
918 dev->video_mode.isoc_ctl.num_bufs = 0;
919 dma_q->p_left_data = NULL;
921 if (dev->mode_tv == 0)
922 cx231xx_capture_start(dev, 0, Raw_Video);
923 else
924 cx231xx_capture_start(dev, 0, TS1_serial_mode);
928 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
931 * Stop and Deallocate URBs
933 void cx231xx_uninit_bulk(struct cx231xx *dev)
935 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
936 struct urb *urb;
937 int i;
938 bool broken_pipe = false;
940 cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
942 dev->video_mode.bulk_ctl.nfields = -1;
943 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
944 urb = dev->video_mode.bulk_ctl.urb[i];
945 if (urb) {
946 if (!irqs_disabled())
947 usb_kill_urb(urb);
948 else
949 usb_unlink_urb(urb);
951 if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
952 usb_free_coherent(dev->udev,
953 urb->transfer_buffer_length,
954 dev->video_mode.bulk_ctl.
955 transfer_buffer[i],
956 urb->transfer_dma);
958 if (urb->status == -EPIPE) {
959 broken_pipe = true;
961 usb_free_urb(urb);
962 dev->video_mode.bulk_ctl.urb[i] = NULL;
964 dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
967 if (broken_pipe) {
968 cx231xx_isocdbg("Reset endpoint to recover broken pipe.");
969 usb_reset_endpoint(dev->udev, dev->video_mode.end_point_addr);
971 kfree(dev->video_mode.bulk_ctl.urb);
972 kfree(dev->video_mode.bulk_ctl.transfer_buffer);
973 kfree(dma_q->p_left_data);
975 dev->video_mode.bulk_ctl.urb = NULL;
976 dev->video_mode.bulk_ctl.transfer_buffer = NULL;
977 dev->video_mode.bulk_ctl.num_bufs = 0;
978 dma_q->p_left_data = NULL;
980 if (dev->mode_tv == 0)
981 cx231xx_capture_start(dev, 0, Raw_Video);
982 else
983 cx231xx_capture_start(dev, 0, TS1_serial_mode);
987 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);
990 * Allocate URBs and start IRQ
992 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
993 int num_bufs, int max_pkt_size,
994 int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
996 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
997 int i;
998 int sb_size, pipe;
999 struct urb *urb;
1000 int j, k;
1001 int rc;
1003 /* De-allocates all pending stuff */
1004 cx231xx_uninit_isoc(dev);
1006 dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
1007 if (dma_q->p_left_data == NULL)
1008 return -ENOMEM;
1010 dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
1011 dev->video_mode.isoc_ctl.num_bufs = num_bufs;
1012 dma_q->pos = 0;
1013 dma_q->is_partial_line = 0;
1014 dma_q->last_sav = 0;
1015 dma_q->current_field = -1;
1016 dma_q->field1_done = 0;
1017 dma_q->lines_per_field = dev->height / 2;
1018 dma_q->bytes_left_in_line = dev->width << 1;
1019 dma_q->lines_completed = 0;
1020 dma_q->mpeg_buffer_done = 0;
1021 dma_q->left_data_count = 0;
1022 dma_q->mpeg_buffer_completed = 0;
1023 dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
1024 dma_q->ps_head[0] = 0x00;
1025 dma_q->ps_head[1] = 0x00;
1026 dma_q->ps_head[2] = 0x01;
1027 dma_q->ps_head[3] = 0xBA;
1028 for (i = 0; i < 8; i++)
1029 dma_q->partial_buf[i] = 0;
1031 dev->video_mode.isoc_ctl.urb =
1032 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1033 if (!dev->video_mode.isoc_ctl.urb) {
1034 dev_err(dev->dev,
1035 "cannot alloc memory for usb buffers\n");
1036 return -ENOMEM;
1039 dev->video_mode.isoc_ctl.transfer_buffer =
1040 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1041 if (!dev->video_mode.isoc_ctl.transfer_buffer) {
1042 dev_err(dev->dev,
1043 "cannot allocate memory for usbtransfer\n");
1044 kfree(dev->video_mode.isoc_ctl.urb);
1045 return -ENOMEM;
1048 dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
1049 dev->video_mode.isoc_ctl.buf = NULL;
1051 sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
1053 if (dev->mode_tv == 1)
1054 dev->video_mode.end_point_addr = 0x81;
1055 else
1056 dev->video_mode.end_point_addr = 0x84;
1059 /* allocate urbs and transfer buffers */
1060 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1061 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
1062 if (!urb) {
1063 cx231xx_uninit_isoc(dev);
1064 return -ENOMEM;
1066 dev->video_mode.isoc_ctl.urb[i] = urb;
1068 dev->video_mode.isoc_ctl.transfer_buffer[i] =
1069 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1070 &urb->transfer_dma);
1071 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
1072 dev_err(dev->dev,
1073 "unable to allocate %i bytes for transfer buffer %i%s\n",
1074 sb_size, i,
1075 in_interrupt() ? " while in int" : "");
1076 cx231xx_uninit_isoc(dev);
1077 return -ENOMEM;
1079 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
1081 pipe =
1082 usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
1084 usb_fill_int_urb(urb, dev->udev, pipe,
1085 dev->video_mode.isoc_ctl.transfer_buffer[i],
1086 sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
1088 urb->number_of_packets = max_packets;
1089 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
1091 k = 0;
1092 for (j = 0; j < max_packets; j++) {
1093 urb->iso_frame_desc[j].offset = k;
1094 urb->iso_frame_desc[j].length =
1095 dev->video_mode.isoc_ctl.max_pkt_size;
1096 k += dev->video_mode.isoc_ctl.max_pkt_size;
1100 init_waitqueue_head(&dma_q->wq);
1102 /* submit urbs and enables IRQ */
1103 for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1104 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
1105 GFP_ATOMIC);
1106 if (rc) {
1107 dev_err(dev->dev,
1108 "submit of urb %i failed (error=%i)\n", i,
1109 rc);
1110 cx231xx_uninit_isoc(dev);
1111 return rc;
1115 if (dev->mode_tv == 0)
1116 cx231xx_capture_start(dev, 1, Raw_Video);
1117 else
1118 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1120 return 0;
1122 EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
1125 * Allocate URBs and start IRQ
1127 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
1128 int num_bufs, int max_pkt_size,
1129 int (*bulk_copy) (struct cx231xx *dev, struct urb *urb))
1131 struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
1132 int i;
1133 int sb_size, pipe;
1134 struct urb *urb;
1135 int rc;
1137 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1139 cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);
1141 video_mux(dev, dev->video_input);
1143 /* De-allocates all pending stuff */
1144 cx231xx_uninit_bulk(dev);
1146 dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
1147 dev->video_mode.bulk_ctl.num_bufs = num_bufs;
1148 dma_q->pos = 0;
1149 dma_q->is_partial_line = 0;
1150 dma_q->last_sav = 0;
1151 dma_q->current_field = -1;
1152 dma_q->field1_done = 0;
1153 dma_q->lines_per_field = dev->height / 2;
1154 dma_q->bytes_left_in_line = dev->width << 1;
1155 dma_q->lines_completed = 0;
1156 dma_q->mpeg_buffer_done = 0;
1157 dma_q->left_data_count = 0;
1158 dma_q->mpeg_buffer_completed = 0;
1159 dma_q->ps_head[0] = 0x00;
1160 dma_q->ps_head[1] = 0x00;
1161 dma_q->ps_head[2] = 0x01;
1162 dma_q->ps_head[3] = 0xBA;
1163 for (i = 0; i < 8; i++)
1164 dma_q->partial_buf[i] = 0;
1166 dev->video_mode.bulk_ctl.urb =
1167 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1168 if (!dev->video_mode.bulk_ctl.urb) {
1169 dev_err(dev->dev,
1170 "cannot alloc memory for usb buffers\n");
1171 return -ENOMEM;
1174 dev->video_mode.bulk_ctl.transfer_buffer =
1175 kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1176 if (!dev->video_mode.bulk_ctl.transfer_buffer) {
1177 dev_err(dev->dev,
1178 "cannot allocate memory for usbtransfer\n");
1179 kfree(dev->video_mode.bulk_ctl.urb);
1180 return -ENOMEM;
1183 dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
1184 dev->video_mode.bulk_ctl.buf = NULL;
1186 sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;
1188 if (dev->mode_tv == 1)
1189 dev->video_mode.end_point_addr = 0x81;
1190 else
1191 dev->video_mode.end_point_addr = 0x84;
1194 /* allocate urbs and transfer buffers */
1195 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1196 urb = usb_alloc_urb(0, GFP_KERNEL);
1197 if (!urb) {
1198 cx231xx_uninit_bulk(dev);
1199 return -ENOMEM;
1201 dev->video_mode.bulk_ctl.urb[i] = urb;
1202 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1204 dev->video_mode.bulk_ctl.transfer_buffer[i] =
1205 usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1206 &urb->transfer_dma);
1207 if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
1208 dev_err(dev->dev,
1209 "unable to allocate %i bytes for transfer buffer %i%s\n",
1210 sb_size, i,
1211 in_interrupt() ? " while in int" : "");
1212 cx231xx_uninit_bulk(dev);
1213 return -ENOMEM;
1215 memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);
1217 pipe = usb_rcvbulkpipe(dev->udev,
1218 dev->video_mode.end_point_addr);
1219 usb_fill_bulk_urb(urb, dev->udev, pipe,
1220 dev->video_mode.bulk_ctl.transfer_buffer[i],
1221 sb_size, cx231xx_bulk_irq_callback, dma_q);
1224 /* clear halt */
1225 rc = usb_clear_halt(dev->udev, dev->video_mode.bulk_ctl.urb[0]->pipe);
1226 if (rc < 0) {
1227 dev_err(dev->dev,
1228 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1229 rc);
1230 cx231xx_uninit_bulk(dev);
1231 return rc;
1234 init_waitqueue_head(&dma_q->wq);
1236 /* submit urbs and enables IRQ */
1237 for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1238 rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
1239 GFP_ATOMIC);
1240 if (rc) {
1241 dev_err(dev->dev,
1242 "submit of urb %i failed (error=%i)\n", i, rc);
1243 cx231xx_uninit_bulk(dev);
1244 return rc;
1248 if (dev->mode_tv == 0)
1249 cx231xx_capture_start(dev, 1, Raw_Video);
1250 else
1251 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1253 return 0;
1255 EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
1256 void cx231xx_stop_TS1(struct cx231xx *dev)
1258 u8 val[4] = { 0, 0, 0, 0 };
1260 val[0] = 0x00;
1261 val[1] = 0x03;
1262 val[2] = 0x00;
1263 val[3] = 0x00;
1264 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1265 TS_MODE_REG, val, 4);
1267 val[0] = 0x00;
1268 val[1] = 0x70;
1269 val[2] = 0x04;
1270 val[3] = 0x00;
1271 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1272 TS1_CFG_REG, val, 4);
1274 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
1275 void cx231xx_start_TS1(struct cx231xx *dev)
1277 u8 val[4] = { 0, 0, 0, 0 };
1279 val[0] = 0x03;
1280 val[1] = 0x03;
1281 val[2] = 0x00;
1282 val[3] = 0x00;
1283 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1284 TS_MODE_REG, val, 4);
1286 val[0] = 0x04;
1287 val[1] = 0xA3;
1288 val[2] = 0x3B;
1289 val[3] = 0x00;
1290 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1291 TS1_CFG_REG, val, 4);
1293 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
1294 /*****************************************************************
1295 * Device Init/UnInit functions *
1296 ******************************************************************/
1297 int cx231xx_dev_init(struct cx231xx *dev)
1299 int errCode = 0;
1301 /* Initialize I2C bus */
1303 /* External Master 1 Bus */
1304 dev->i2c_bus[0].nr = 0;
1305 dev->i2c_bus[0].dev = dev;
1306 dev->i2c_bus[0].i2c_period = I2C_SPEED_100K; /* 100 KHz */
1307 dev->i2c_bus[0].i2c_nostop = 0;
1308 dev->i2c_bus[0].i2c_reserve = 0;
1310 /* External Master 2 Bus */
1311 dev->i2c_bus[1].nr = 1;
1312 dev->i2c_bus[1].dev = dev;
1313 dev->i2c_bus[1].i2c_period = I2C_SPEED_100K; /* 100 KHz */
1314 dev->i2c_bus[1].i2c_nostop = 0;
1315 dev->i2c_bus[1].i2c_reserve = 0;
1317 /* Internal Master 3 Bus */
1318 dev->i2c_bus[2].nr = 2;
1319 dev->i2c_bus[2].dev = dev;
1320 dev->i2c_bus[2].i2c_period = I2C_SPEED_100K; /* 100kHz */
1321 dev->i2c_bus[2].i2c_nostop = 0;
1322 dev->i2c_bus[2].i2c_reserve = 0;
1324 /* register I2C buses */
1325 errCode = cx231xx_i2c_register(&dev->i2c_bus[0]);
1326 if (errCode < 0)
1327 return errCode;
1328 errCode = cx231xx_i2c_register(&dev->i2c_bus[1]);
1329 if (errCode < 0)
1330 return errCode;
1331 errCode = cx231xx_i2c_register(&dev->i2c_bus[2]);
1332 if (errCode < 0)
1333 return errCode;
1335 errCode = cx231xx_i2c_mux_create(dev);
1336 if (errCode < 0) {
1337 dev_err(dev->dev,
1338 "%s: Failed to create I2C mux\n", __func__);
1339 return errCode;
1341 errCode = cx231xx_i2c_mux_register(dev, 0);
1342 if (errCode < 0)
1343 return errCode;
1345 errCode = cx231xx_i2c_mux_register(dev, 1);
1346 if (errCode < 0)
1347 return errCode;
1349 /* scan the real bus segments in the order of physical port numbers */
1350 cx231xx_do_i2c_scan(dev, I2C_0);
1351 cx231xx_do_i2c_scan(dev, I2C_1_MUX_1);
1352 cx231xx_do_i2c_scan(dev, I2C_2);
1353 cx231xx_do_i2c_scan(dev, I2C_1_MUX_3);
1355 /* init hardware */
1356 /* Note : with out calling set power mode function,
1357 afe can not be set up correctly */
1358 if (dev->board.external_av) {
1359 errCode = cx231xx_set_power_mode(dev,
1360 POLARIS_AVMODE_ENXTERNAL_AV);
1361 if (errCode < 0) {
1362 dev_err(dev->dev,
1363 "%s: Failed to set Power - errCode [%d]!\n",
1364 __func__, errCode);
1365 return errCode;
1367 } else {
1368 errCode = cx231xx_set_power_mode(dev,
1369 POLARIS_AVMODE_ANALOGT_TV);
1370 if (errCode < 0) {
1371 dev_err(dev->dev,
1372 "%s: Failed to set Power - errCode [%d]!\n",
1373 __func__, errCode);
1374 return errCode;
1378 /* reset the Tuner, if it is a Xceive tuner */
1379 if ((dev->board.tuner_type == TUNER_XC5000) ||
1380 (dev->board.tuner_type == TUNER_XC2028))
1381 cx231xx_gpio_set(dev, dev->board.tuner_gpio);
1383 /* initialize Colibri block */
1384 errCode = cx231xx_afe_init_super_block(dev, 0x23c);
1385 if (errCode < 0) {
1386 dev_err(dev->dev,
1387 "%s: cx231xx_afe init super block - errCode [%d]!\n",
1388 __func__, errCode);
1389 return errCode;
1391 errCode = cx231xx_afe_init_channels(dev);
1392 if (errCode < 0) {
1393 dev_err(dev->dev,
1394 "%s: cx231xx_afe init channels - errCode [%d]!\n",
1395 __func__, errCode);
1396 return errCode;
1399 /* Set DIF in By pass mode */
1400 errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
1401 if (errCode < 0) {
1402 dev_err(dev->dev,
1403 "%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
1404 __func__, errCode);
1405 return errCode;
1408 /* I2S block related functions */
1409 errCode = cx231xx_i2s_blk_initialize(dev);
1410 if (errCode < 0) {
1411 dev_err(dev->dev,
1412 "%s: cx231xx_i2s block initialize - errCode [%d]!\n",
1413 __func__, errCode);
1414 return errCode;
1417 /* init control pins */
1418 errCode = cx231xx_init_ctrl_pin_status(dev);
1419 if (errCode < 0) {
1420 dev_err(dev->dev,
1421 "%s: cx231xx_init ctrl pins - errCode [%d]!\n",
1422 __func__, errCode);
1423 return errCode;
1426 /* set AGC mode to Analog */
1427 switch (dev->model) {
1428 case CX231XX_BOARD_CNXT_CARRAERA:
1429 case CX231XX_BOARD_CNXT_RDE_250:
1430 case CX231XX_BOARD_CNXT_SHELBY:
1431 case CX231XX_BOARD_CNXT_RDU_250:
1432 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
1433 break;
1434 case CX231XX_BOARD_CNXT_RDE_253S:
1435 case CX231XX_BOARD_CNXT_RDU_253S:
1436 case CX231XX_BOARD_HAUPPAUGE_EXETER:
1437 case CX231XX_BOARD_HAUPPAUGE_930C_HD_1113xx:
1438 case CX231XX_BOARD_PV_PLAYTV_USB_HYBRID:
1439 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_PAL:
1440 case CX231XX_BOARD_HAUPPAUGE_USB2_FM_NTSC:
1441 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
1442 break;
1443 default:
1444 break;
1446 if (errCode < 0) {
1447 dev_err(dev->dev,
1448 "%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
1449 __func__, errCode);
1450 return errCode;
1453 /* set all alternate settings to zero initially */
1454 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1455 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1456 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1457 if (dev->board.has_dvb)
1458 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1460 errCode = 0;
1461 return errCode;
1463 EXPORT_SYMBOL_GPL(cx231xx_dev_init);
1465 void cx231xx_dev_uninit(struct cx231xx *dev)
1467 /* Un Initialize I2C bus */
1468 cx231xx_i2c_mux_unregister(dev);
1469 cx231xx_i2c_unregister(&dev->i2c_bus[2]);
1470 cx231xx_i2c_unregister(&dev->i2c_bus[1]);
1471 cx231xx_i2c_unregister(&dev->i2c_bus[0]);
1473 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
1475 /*****************************************************************
1476 * G P I O related functions *
1477 ******************************************************************/
1478 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
1479 u8 len, u8 request, u8 direction)
1481 int status = 0;
1482 struct VENDOR_REQUEST_IN ven_req;
1484 /* Set wValue */
1485 ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
1487 /* set request */
1488 if (!request) {
1489 if (direction)
1490 ven_req.bRequest = VRT_GET_GPIO; /* 0x9 gpio */
1491 else
1492 ven_req.bRequest = VRT_SET_GPIO; /* 0x8 gpio */
1493 } else {
1494 if (direction)
1495 ven_req.bRequest = VRT_GET_GPIE; /* 0xb gpie */
1496 else
1497 ven_req.bRequest = VRT_SET_GPIE; /* 0xa gpie */
1500 /* set index value */
1501 ven_req.wIndex = (u16) (gpio_bit & 0xffff);
1503 /* set wLength value */
1504 ven_req.wLength = len;
1506 /* set bData value */
1507 ven_req.bData = 0;
1509 /* set the buffer for read / write */
1510 ven_req.pBuff = gpio_val;
1512 /* set the direction */
1513 if (direction) {
1514 ven_req.direction = USB_DIR_IN;
1515 memset(ven_req.pBuff, 0x00, ven_req.wLength);
1516 } else
1517 ven_req.direction = USB_DIR_OUT;
1520 /* call common vendor command request */
1521 status = cx231xx_send_vendor_cmd(dev, &ven_req);
1522 if (status < 0) {
1523 dev_err(dev->dev, "%s: failed with status -%d\n",
1524 __func__, status);
1527 return status;
1529 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
1531 /*****************************************************************
1532 * C O N T R O L - Register R E A D / W R I T E functions *
1533 *****************************************************************/
1534 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1536 u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1537 u32 tmp = 0;
1538 int status = 0;
1540 status =
1541 cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1542 if (status < 0)
1543 return status;
1545 tmp = le32_to_cpu(*((__le32 *) value));
1546 tmp |= mode;
1548 value[0] = (u8) tmp;
1549 value[1] = (u8) (tmp >> 8);
1550 value[2] = (u8) (tmp >> 16);
1551 value[3] = (u8) (tmp >> 24);
1553 status =
1554 cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1556 return status;
1559 /*****************************************************************
1560 * I 2 C Internal C O N T R O L functions *
1561 *****************************************************************/
1562 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1563 u8 saddr_len, u32 *data, u8 data_len, int master)
1565 int status = 0;
1566 struct cx231xx_i2c_xfer_data req_data;
1567 u8 value[64] = "0";
1569 if (saddr_len == 0)
1570 saddr = 0;
1571 else if (saddr_len == 1)
1572 saddr &= 0xff;
1574 /* prepare xfer_data struct */
1575 req_data.dev_addr = dev_addr >> 1;
1576 req_data.direction = I2C_M_RD;
1577 req_data.saddr_len = saddr_len;
1578 req_data.saddr_dat = saddr;
1579 req_data.buf_size = data_len;
1580 req_data.p_buffer = (u8 *) value;
1582 /* usb send command */
1583 if (master == 0)
1584 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1585 &req_data);
1586 else if (master == 1)
1587 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1588 &req_data);
1589 else if (master == 2)
1590 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1591 &req_data);
1593 if (status >= 0) {
1594 /* Copy the data read back to main buffer */
1595 if (data_len == 1)
1596 *data = value[0];
1597 else if (data_len == 4)
1598 *data =
1599 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1600 << 24;
1601 else if (data_len > 4)
1602 *data = value[saddr];
1605 return status;
1608 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1609 u8 saddr_len, u32 data, u8 data_len, int master)
1611 int status = 0;
1612 u8 value[4] = { 0, 0, 0, 0 };
1613 struct cx231xx_i2c_xfer_data req_data;
1615 value[0] = (u8) data;
1616 value[1] = (u8) (data >> 8);
1617 value[2] = (u8) (data >> 16);
1618 value[3] = (u8) (data >> 24);
1620 if (saddr_len == 0)
1621 saddr = 0;
1622 else if (saddr_len == 1)
1623 saddr &= 0xff;
1625 /* prepare xfer_data struct */
1626 req_data.dev_addr = dev_addr >> 1;
1627 req_data.direction = 0;
1628 req_data.saddr_len = saddr_len;
1629 req_data.saddr_dat = saddr;
1630 req_data.buf_size = data_len;
1631 req_data.p_buffer = value;
1633 /* usb send command */
1634 if (master == 0)
1635 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1636 &req_data);
1637 else if (master == 1)
1638 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1639 &req_data);
1640 else if (master == 2)
1641 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1642 &req_data);
1644 return status;
1647 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1648 u8 saddr_len, u32 *data, u8 data_len)
1650 int status = 0;
1651 struct cx231xx_i2c_xfer_data req_data;
1652 u8 value[4] = { 0, 0, 0, 0 };
1654 if (saddr_len == 0)
1655 saddr = 0;
1656 else if (saddr_len == 1)
1657 saddr &= 0xff;
1659 /* prepare xfer_data struct */
1660 req_data.dev_addr = dev_addr >> 1;
1661 req_data.direction = I2C_M_RD;
1662 req_data.saddr_len = saddr_len;
1663 req_data.saddr_dat = saddr;
1664 req_data.buf_size = data_len;
1665 req_data.p_buffer = (u8 *) value;
1667 /* usb send command */
1668 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1670 if (status >= 0) {
1671 /* Copy the data read back to main buffer */
1672 if (data_len == 1)
1673 *data = value[0];
1674 else
1675 *data =
1676 value[0] | value[1] << 8 | value[2] << 16 | value[3]
1677 << 24;
1680 return status;
1683 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1684 u8 saddr_len, u32 data, u8 data_len)
1686 int status = 0;
1687 u8 value[4] = { 0, 0, 0, 0 };
1688 struct cx231xx_i2c_xfer_data req_data;
1690 value[0] = (u8) data;
1691 value[1] = (u8) (data >> 8);
1692 value[2] = (u8) (data >> 16);
1693 value[3] = (u8) (data >> 24);
1695 if (saddr_len == 0)
1696 saddr = 0;
1697 else if (saddr_len == 1)
1698 saddr &= 0xff;
1700 /* prepare xfer_data struct */
1701 req_data.dev_addr = dev_addr >> 1;
1702 req_data.direction = 0;
1703 req_data.saddr_len = saddr_len;
1704 req_data.saddr_dat = saddr;
1705 req_data.buf_size = data_len;
1706 req_data.p_buffer = value;
1708 /* usb send command */
1709 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1711 return status;
1714 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1715 u16 register_address, u8 bit_start, u8 bit_end,
1716 u32 value)
1718 int status = 0;
1719 u32 tmp;
1720 u32 mask = 0;
1721 int i;
1723 if (bit_start > (size - 1) || bit_end > (size - 1))
1724 return -1;
1726 if (size == 8) {
1727 status =
1728 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1729 &tmp, 1);
1730 } else {
1731 status =
1732 cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1733 &tmp, 4);
1736 if (status < 0)
1737 return status;
1739 mask = 1 << bit_end;
1740 for (i = bit_end; i > bit_start && i > 0; i--)
1741 mask = mask + (1 << (i - 1));
1743 value <<= bit_start;
1745 if (size == 8) {
1746 tmp &= ~mask;
1747 tmp |= value;
1748 tmp &= 0xff;
1749 status =
1750 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1751 tmp, 1);
1752 } else {
1753 tmp &= ~mask;
1754 tmp |= value;
1755 status =
1756 cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1757 tmp, 4);
1760 return status;
1763 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1764 u16 saddr, u32 mask, u32 value)
1766 u32 temp;
1767 int status = 0;
1769 status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1771 if (status < 0)
1772 return status;
1774 temp &= ~mask;
1775 temp |= value;
1777 status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1779 return status;
1782 u32 cx231xx_set_field(u32 field_mask, u32 data)
1784 u32 temp;
1786 for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1787 data <<= 1;
1789 return data;