Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/media/video/cx231xx/cx231xx-core.c

  1 /*
  2    cx231xx-core.c - driver for Conexant Cx23100/101/102
  3                                 USB video capture devices
  4 
  5    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  6                                 Based on em28xx driver
  7 
  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.
 12 
 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.
 17 
 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.
 21  */
 22 
 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>
 30 #include <media/tuner.h>
 31 
 32 #include "cx231xx.h"
 33 #include "cx231xx-reg.h"
 34 
 35 /* #define ENABLE_DEBUG_ISOC_FRAMES */
 36 
 37 static unsigned int core_debug;
 38 module_param(core_debug, int, 0644);
 39 MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
 40 
 41 #define cx231xx_coredbg(fmt, arg...) do {\
 42         if (core_debug) \
 43                 printk(KERN_INFO "%s %s :"fmt, \
 44                          dev->name, __func__ , ##arg); } while (0)
 45 
 46 static unsigned int reg_debug;
 47 module_param(reg_debug, int, 0644);
 48 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
 49 
 50 static int alt = CX231XX_PINOUT;
 51 module_param(alt, int, 0644);
 52 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
 53 
 54 #define cx231xx_isocdbg(fmt, arg...) do {\
 55         if (core_debug) \
 56                 printk(KERN_INFO "%s %s :"fmt, \
 57                          dev->name, __func__ , ##arg); } while (0)
 58 
 59 /*****************************************************************
 60 *             Device control list functions                                      *
 61 ******************************************************************/
 62 
 63 LIST_HEAD(cx231xx_devlist);
 64 static DEFINE_MUTEX(cx231xx_devlist_mutex);
 65 
 66 /*
 67  * cx231xx_realease_resources()
 68  * unregisters the v4l2,i2c and usb devices
 69  * called when the device gets disconected or at module unload
 70 */
 71 void cx231xx_remove_from_devlist(struct cx231xx *dev)
 72 {
 73         if (dev == NULL)
 74                 return;
 75         if (dev->udev == NULL)
 76                 return;
 77 
 78         if (atomic_read(&dev->devlist_count) > 0) {
 79                 mutex_lock(&cx231xx_devlist_mutex);
 80                 list_del(&dev->devlist);
 81                 atomic_dec(&dev->devlist_count);
 82                 mutex_unlock(&cx231xx_devlist_mutex);
 83         }
 84 };
 85 
 86 void cx231xx_add_into_devlist(struct cx231xx *dev)
 87 {
 88         mutex_lock(&cx231xx_devlist_mutex);
 89         list_add_tail(&dev->devlist, &cx231xx_devlist);
 90         atomic_inc(&dev->devlist_count);
 91         mutex_unlock(&cx231xx_devlist_mutex);
 92 };
 93 
 94 static LIST_HEAD(cx231xx_extension_devlist);
 95 
 96 int cx231xx_register_extension(struct cx231xx_ops *ops)
 97 {
 98         struct cx231xx *dev = NULL;
 99 
100         mutex_lock(&cx231xx_devlist_mutex);
101         list_add_tail(&ops->next, &cx231xx_extension_devlist);
102         list_for_each_entry(dev, &cx231xx_devlist, devlist)
103                 ops->init(dev);
104 
105         printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
106         mutex_unlock(&cx231xx_devlist_mutex);
107         return 0;
108 }
109 EXPORT_SYMBOL(cx231xx_register_extension);
110 
111 void cx231xx_unregister_extension(struct cx231xx_ops *ops)
112 {
113         struct cx231xx *dev = NULL;
114 
115         mutex_lock(&cx231xx_devlist_mutex);
116         list_for_each_entry(dev, &cx231xx_devlist, devlist)
117                 ops->fini(dev);
118 
119 
120         printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
121         list_del(&ops->next);
122         mutex_unlock(&cx231xx_devlist_mutex);
123 }
124 EXPORT_SYMBOL(cx231xx_unregister_extension);
125 
126 void cx231xx_init_extension(struct cx231xx *dev)
127 {
128         struct cx231xx_ops *ops = NULL;
129 
130         mutex_lock(&cx231xx_devlist_mutex);
131         if (!list_empty(&cx231xx_extension_devlist)) {
132                 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
133                         if (ops->init)
134                                 ops->init(dev);
135                 }
136         }
137         mutex_unlock(&cx231xx_devlist_mutex);
138 }
139 
140 void cx231xx_close_extension(struct cx231xx *dev)
141 {
142         struct cx231xx_ops *ops = NULL;
143 
144         mutex_lock(&cx231xx_devlist_mutex);
145         if (!list_empty(&cx231xx_extension_devlist)) {
146                 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
147                         if (ops->fini)
148                                 ops->fini(dev);
149                 }
150         }
151         mutex_unlock(&cx231xx_devlist_mutex);
152 }
153 
154 /****************************************************************
155 *               U S B related functions                         *
156 *****************************************************************/
157 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
158                              struct cx231xx_i2c_xfer_data *req_data)
159 {
160         int status = 0;
161         struct cx231xx *dev = i2c_bus->dev;
162         struct VENDOR_REQUEST_IN ven_req;
163 
164         u8 saddr_len = 0;
165         u8 _i2c_period = 0;
166         u8 _i2c_nostop = 0;
167         u8 _i2c_reserve = 0;
168 
169         /* Get the I2C period, nostop and reserve parameters */
170         _i2c_period = i2c_bus->i2c_period;
171         _i2c_nostop = i2c_bus->i2c_nostop;
172         _i2c_reserve = i2c_bus->i2c_reserve;
173 
174         saddr_len = req_data->saddr_len;
175 
176         /* Set wValue */
177         if (saddr_len == 1)     /* need check saddr_len == 0  */
178                 ven_req.wValue =
179                     req_data->
180                     dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
181                     _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
182         else
183                 ven_req.wValue =
184                     req_data->
185                     dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
186                     _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
187 
188         /* set channel number */
189         if (req_data->direction & I2C_M_RD) {
190                 /* channel number, for read,spec required channel_num +4 */
191                 ven_req.bRequest = i2c_bus->nr + 4;
192         } else
193                 ven_req.bRequest = i2c_bus->nr; /* channel number,  */
194 
195         /* set index value */
196         switch (saddr_len) {
197         case 0:
198                 ven_req.wIndex = 0;     /* need check */
199                 break;
200         case 1:
201                 ven_req.wIndex = (req_data->saddr_dat & 0xff);
202                 break;
203         case 2:
204                 ven_req.wIndex = req_data->saddr_dat;
205                 break;
206         }
207 
208         /* set wLength value */
209         ven_req.wLength = req_data->buf_size;
210 
211         /* set bData value */
212         ven_req.bData = 0;
213 
214         /* set the direction */
215         if (req_data->direction) {
216                 ven_req.direction = USB_DIR_IN;
217                 memset(req_data->p_buffer, 0x00, ven_req.wLength);
218         } else
219                 ven_req.direction = USB_DIR_OUT;
220 
221         /* set the buffer for read / write */
222         ven_req.pBuff = req_data->p_buffer;
223 
224 
225         /* call common vendor command request */
226         status = cx231xx_send_vendor_cmd(dev, &ven_req);
227         if (status < 0) {
228                 cx231xx_info
229                     ("UsbInterface::sendCommand, failed with status -%d\n",
230                      status);
231         }
232 
233         return status;
234 }
235 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
236 
237 /*
238  * Sends/Receives URB control messages, assuring to use a kalloced buffer
239  * for all operations (dev->urb_buf), to avoid using stacked buffers, as
240  * they aren't safe for usage with USB, due to DMA restrictions.
241  * Also implements the debug code for control URB's.
242  */
243 static int __usb_control_msg(struct cx231xx *dev, unsigned int pipe,
244         __u8 request, __u8 requesttype, __u16 value, __u16 index,
245         void *data, __u16 size, int timeout)
246 {
247         int rc, i;
248 
249         if (reg_debug) {
250                 printk(KERN_DEBUG "%s: (pipe 0x%08x): "
251                                 "%s:  %02x %02x %02x %02x %02x %02x %02x %02x ",
252                                 dev->name,
253                                 pipe,
254                                 (requesttype & USB_DIR_IN) ? "IN" : "OUT",
255                                 requesttype,
256                                 request,
257                                 value & 0xff, value >> 8,
258                                 index & 0xff, index >> 8,
259                                 size & 0xff, size >> 8);
260                 if (!(requesttype & USB_DIR_IN)) {
261                         printk(KERN_CONT ">>>");
262                         for (i = 0; i < size; i++)
263                                 printk(KERN_CONT " %02x",
264                                        ((unsigned char *)data)[i]);
265                 }
266         }
267 
268         /* Do the real call to usb_control_msg */
269         mutex_lock(&dev->ctrl_urb_lock);
270         if (!(requesttype & USB_DIR_IN) && size)
271                 memcpy(dev->urb_buf, data, size);
272         rc = usb_control_msg(dev->udev, pipe, request, requesttype, value,
273                              index, dev->urb_buf, size, timeout);
274         if ((requesttype & USB_DIR_IN) && size)
275                 memcpy(data, dev->urb_buf, size);
276         mutex_unlock(&dev->ctrl_urb_lock);
277 
278         if (reg_debug) {
279                 if (unlikely(rc < 0)) {
280                         printk(KERN_CONT "FAILED!\n");
281                         return rc;
282                 }
283 
284                 if ((requesttype & USB_DIR_IN)) {
285                         printk(KERN_CONT "<<<");
286                         for (i = 0; i < size; i++)
287                                 printk(KERN_CONT " %02x",
288                                        ((unsigned char *)data)[i]);
289                 }
290                 printk(KERN_CONT "\n");
291         }
292 
293         return rc;
294 }
295 
296 
297 /*
298  * cx231xx_read_ctrl_reg()
299  * reads data from the usb device specifying bRequest and wValue
300  */
301 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
302                           char *buf, int len)
303 {
304         u8 val = 0;
305         int ret;
306         int pipe = usb_rcvctrlpipe(dev->udev, 0);
307 
308         if (dev->state & DEV_DISCONNECTED)
309                 return -ENODEV;
310 
311         if (len > URB_MAX_CTRL_SIZE)
312                 return -EINVAL;
313 
314         switch (len) {
315         case 1:
316                 val = ENABLE_ONE_BYTE;
317                 break;
318         case 2:
319                 val = ENABLE_TWE_BYTE;
320                 break;
321         case 3:
322                 val = ENABLE_THREE_BYTE;
323                 break;
324         case 4:
325                 val = ENABLE_FOUR_BYTE;
326                 break;
327         default:
328                 val = 0xFF;     /* invalid option */
329         }
330 
331         if (val == 0xFF)
332                 return -EINVAL;
333 
334         ret = __usb_control_msg(dev, pipe, req,
335                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
336                               val, reg, buf, len, HZ);
337         return ret;
338 }
339 
340 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
341                                 struct VENDOR_REQUEST_IN *ven_req)
342 {
343         int ret;
344         int pipe = 0;
345         int unsend_size = 0;
346         u8 *pdata;
347 
348         if (dev->state & DEV_DISCONNECTED)
349                 return -ENODEV;
350 
351         if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
352                 return -EINVAL;
353 
354         if (ven_req->direction)
355                 pipe = usb_rcvctrlpipe(dev->udev, 0);
356         else
357                 pipe = usb_sndctrlpipe(dev->udev, 0);
358 
359         /*
360          * If the cx23102 read more than 4 bytes with i2c bus,
361          * need chop to 4 byte per request
362          */
363         if ((ven_req->wLength > 4) && ((ven_req->bRequest == 0x4) ||
364                                         (ven_req->bRequest == 0x5) ||
365                                         (ven_req->bRequest == 0x6))) {
366                 unsend_size = 0;
367                 pdata = ven_req->pBuff;
368 
369 
370                 unsend_size = ven_req->wLength;
371 
372                 /* the first package */
373                 ven_req->wValue = ven_req->wValue & 0xFFFB;
374                 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x2;
375                 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
376                         ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
377                         ven_req->wValue, ven_req->wIndex, pdata,
378                         0x0004, HZ);
379                 unsend_size = unsend_size - 4;
380 
381                 /* the middle package */
382                 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x42;
383                 while (unsend_size - 4 > 0) {
384                         pdata = pdata + 4;
385                         ret = __usb_control_msg(dev, pipe,
386                                 ven_req->bRequest,
387                                 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
388                                 ven_req->wValue, ven_req->wIndex, pdata,
389                                 0x0004, HZ);
390                         unsend_size = unsend_size - 4;
391                 }
392 
393                 /* the last package */
394                 ven_req->wValue = (ven_req->wValue & 0xFFBD) | 0x40;
395                 pdata = pdata + 4;
396                 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
397                         ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
398                         ven_req->wValue, ven_req->wIndex, pdata,
399                         unsend_size, HZ);
400         } else {
401                 ret = __usb_control_msg(dev, pipe, ven_req->bRequest,
402                                 ven_req->direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
403                                 ven_req->wValue, ven_req->wIndex,
404                                 ven_req->pBuff, ven_req->wLength, HZ);
405         }
406 
407         return ret;
408 }
409 
410 /*
411  * cx231xx_write_ctrl_reg()
412  * sends data to the usb device, specifying bRequest
413  */
414 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
415                            int len)
416 {
417         u8 val = 0;
418         int ret;
419         int pipe = usb_sndctrlpipe(dev->udev, 0);
420 
421         if (dev->state & DEV_DISCONNECTED)
422                 return -ENODEV;
423 
424         if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
425                 return -EINVAL;
426 
427         switch (len) {
428         case 1:
429                 val = ENABLE_ONE_BYTE;
430                 break;
431         case 2:
432                 val = ENABLE_TWE_BYTE;
433                 break;
434         case 3:
435                 val = ENABLE_THREE_BYTE;
436                 break;
437         case 4:
438                 val = ENABLE_FOUR_BYTE;
439                 break;
440         default:
441                 val = 0xFF;     /* invalid option */
442         }
443 
444         if (val == 0xFF)
445                 return -EINVAL;
446 
447         if (reg_debug) {
448                 int byte;
449 
450                 cx231xx_isocdbg("(pipe 0x%08x): "
451                         "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
452                         pipe,
453                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454                         req, 0, val, reg & 0xff,
455                         reg >> 8, len & 0xff, len >> 8);
456 
457                 for (byte = 0; byte < len; byte++)
458                         cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
459                 cx231xx_isocdbg("\n");
460         }
461 
462         ret = __usb_control_msg(dev, pipe, req,
463                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
464                               val, reg, buf, len, HZ);
465 
466         return ret;
467 }
468 
469 /****************************************************************
470 *           USB Alternate Setting functions                     *
471 *****************************************************************/
472 
473 int cx231xx_set_video_alternate(struct cx231xx *dev)
474 {
475         int errCode, prev_alt = dev->video_mode.alt;
476         unsigned int min_pkt_size = dev->width * 2 + 4;
477         u32 usb_interface_index = 0;
478 
479         /* When image size is bigger than a certain value,
480            the frame size should be increased, otherwise, only
481            green screen will be received.
482          */
483         if (dev->width * 2 * dev->height > 720 * 240 * 2)
484                 min_pkt_size *= 2;
485 
486         if (dev->width > 360) {
487                 /* resolutions: 720,704,640 */
488                 dev->video_mode.alt = 3;
489         } else if (dev->width > 180) {
490                 /* resolutions: 360,352,320,240 */
491                 dev->video_mode.alt = 2;
492         } else if (dev->width > 0) {
493                 /* resolutions: 180,176,160,128,88 */
494                 dev->video_mode.alt = 1;
495         } else {
496                 /* Change to alt0 BULK to release USB bandwidth */
497                 dev->video_mode.alt = 0;
498         }
499 
500         if (dev->USE_ISO == 0)
501                 dev->video_mode.alt = 0;
502 
503         cx231xx_coredbg("dev->video_mode.alt= %d\n", dev->video_mode.alt);
504 
505         /* Get the correct video interface Index */
506         usb_interface_index =
507             dev->current_pcb_config.hs_config_info[0].interface_info.
508             video_index + 1;
509 
510         if (dev->video_mode.alt != prev_alt) {
511                 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
512                                 min_pkt_size, dev->video_mode.alt);
513 
514                 if (dev->video_mode.alt_max_pkt_size != NULL)
515                         dev->video_mode.max_pkt_size =
516                         dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
517                 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
518                                 dev->video_mode.alt,
519                                 dev->video_mode.max_pkt_size);
520                 errCode =
521                     usb_set_interface(dev->udev, usb_interface_index,
522                                       dev->video_mode.alt);
523                 if (errCode < 0) {
524                         cx231xx_errdev
525                             ("cannot change alt number to %d (error=%i)\n",
526                              dev->video_mode.alt, errCode);
527                         return errCode;
528                 }
529         }
530         return 0;
531 }
532 
533 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
534 {
535         int status = 0;
536         u32 usb_interface_index = 0;
537         u32 max_pkt_size = 0;
538 
539         switch (index) {
540         case INDEX_TS1:
541                 usb_interface_index =
542                     dev->current_pcb_config.hs_config_info[0].interface_info.
543                     ts1_index + 1;
544                 dev->ts1_mode.alt = alt;
545                 if (dev->ts1_mode.alt_max_pkt_size != NULL)
546                         max_pkt_size = dev->ts1_mode.max_pkt_size =
547                             dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
548                 break;
549         case INDEX_TS2:
550                 usb_interface_index =
551                     dev->current_pcb_config.hs_config_info[0].interface_info.
552                     ts2_index + 1;
553                 break;
554         case INDEX_AUDIO:
555                 usb_interface_index =
556                     dev->current_pcb_config.hs_config_info[0].interface_info.
557                     audio_index + 1;
558                 dev->adev.alt = alt;
559                 if (dev->adev.alt_max_pkt_size != NULL)
560                         max_pkt_size = dev->adev.max_pkt_size =
561                             dev->adev.alt_max_pkt_size[dev->adev.alt];
562                 break;
563         case INDEX_VIDEO:
564                 usb_interface_index =
565                     dev->current_pcb_config.hs_config_info[0].interface_info.
566                     video_index + 1;
567                 dev->video_mode.alt = alt;
568                 if (dev->video_mode.alt_max_pkt_size != NULL)
569                         max_pkt_size = dev->video_mode.max_pkt_size =
570                             dev->video_mode.alt_max_pkt_size[dev->video_mode.
571                                                              alt];
572                 break;
573         case INDEX_VANC:
574                 usb_interface_index =
575                     dev->current_pcb_config.hs_config_info[0].interface_info.
576                     vanc_index + 1;
577                 dev->vbi_mode.alt = alt;
578                 if (dev->vbi_mode.alt_max_pkt_size != NULL)
579                         max_pkt_size = dev->vbi_mode.max_pkt_size =
580                             dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
581                 break;
582         case INDEX_HANC:
583                 usb_interface_index =
584                     dev->current_pcb_config.hs_config_info[0].interface_info.
585                     hanc_index + 1;
586                 dev->sliced_cc_mode.alt = alt;
587                 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
588                         max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
589                             dev->sliced_cc_mode.alt_max_pkt_size[dev->
590                                                                  sliced_cc_mode.
591                                                                  alt];
592                 break;
593         default:
594                 break;
595         }
596 
597         if (alt > 0 && max_pkt_size == 0) {
598                 cx231xx_errdev
599                 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
600                 usb_interface_index, alt);
601                 /*To workaround error number=-71 on EP0 for videograbber,
602                  need add following codes.*/
603                 if (dev->model != CX231XX_BOARD_CNXT_VIDEO_GRABBER &&
604                     dev->model != CX231XX_BOARD_HAUPPAUGE_USBLIVE2)
605                         return -1;
606         }
607 
608         cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u,"
609                         "Interface = %d\n", alt, max_pkt_size,
610                         usb_interface_index);
611 
612         if (usb_interface_index > 0) {
613                 status = usb_set_interface(dev->udev, usb_interface_index, alt);
614                 if (status < 0) {
615                         cx231xx_errdev
616                         ("can't change interface %d alt no. to %d (err=%i)\n",
617                         usb_interface_index, alt, status);
618                         return status;
619                 }
620         }
621 
622         return status;
623 }
624 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
625 
626 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
627 {
628         int rc = 0;
629 
630         if (!gpio)
631                 return rc;
632 
633         /* Send GPIO reset sequences specified at board entry */
634         while (gpio->sleep >= 0) {
635                 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
636                 if (rc < 0)
637                         return rc;
638 
639                 if (gpio->sleep > 0)
640                         msleep(gpio->sleep);
641 
642                 gpio++;
643         }
644         return rc;
645 }
646 
647 int cx231xx_demod_reset(struct cx231xx *dev)
648 {
649 
650         u8 status = 0;
651         u8 value[4] = { 0, 0, 0, 0 };
652 
653         status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
654                                  value, 4);
655 
656         cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
657                         value[0], value[1], value[2], value[3]);
658 
659         cx231xx_coredbg("Enter cx231xx_demod_reset()\n");
660 
661                 value[1] = (u8) 0x3;
662                 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
663                                                 PWR_CTL_EN, value, 4);
664                         msleep(10);
665 
666                 value[1] = (u8) 0x0;
667                 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
668                                                 PWR_CTL_EN, value, 4);
669                         msleep(10);
670 
671                 value[1] = (u8) 0x3;
672                 status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
673                                                 PWR_CTL_EN, value, 4);
674                         msleep(10);
675 
676 
677 
678         status = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, PWR_CTL_EN,
679                                  value, 4);
680 
681         cx231xx_coredbg("reg0x%x=0x%x 0x%x 0x%x 0x%x\n", PWR_CTL_EN,
682                         value[0], value[1], value[2], value[3]);
683 
684         return status;
685 }
686 EXPORT_SYMBOL_GPL(cx231xx_demod_reset);
687 int is_fw_load(struct cx231xx *dev)
688 {
689         return cx231xx_check_fw(dev);
690 }
691 EXPORT_SYMBOL_GPL(is_fw_load);
692 
693 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
694 {
695         int errCode = 0;
696 
697         if (dev->mode == set_mode)
698                 return 0;
699 
700         if (set_mode == CX231XX_SUSPEND) {
701                 /* Set the chip in power saving mode */
702                 dev->mode = set_mode;
703         }
704 
705         /* Resource is locked */
706         if (dev->mode != CX231XX_SUSPEND)
707                 return -EINVAL;
708 
709         dev->mode = set_mode;
710 
711         if (dev->mode == CX231XX_DIGITAL_MODE)/* Set Digital power mode */ {
712         /* set AGC mode to Digital */
713                 switch (dev->model) {
714                 case CX231XX_BOARD_CNXT_CARRAERA:
715                 case CX231XX_BOARD_CNXT_RDE_250:
716                 case CX231XX_BOARD_CNXT_SHELBY:
717                 case CX231XX_BOARD_CNXT_RDU_250:
718                 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
719                         break;
720                 case CX231XX_BOARD_CNXT_RDE_253S:
721                 case CX231XX_BOARD_CNXT_RDU_253S:
722                         errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
723                         break;
724                 case CX231XX_BOARD_HAUPPAUGE_EXETER:
725                         errCode = cx231xx_set_power_mode(dev,
726                                                 POLARIS_AVMODE_DIGITAL);
727                         break;
728                 default:
729                         break;
730                 }
731         } else/* Set Analog Power mode */ {
732         /* set AGC mode to Analog */
733                 switch (dev->model) {
734                 case CX231XX_BOARD_CNXT_CARRAERA:
735                 case CX231XX_BOARD_CNXT_RDE_250:
736                 case CX231XX_BOARD_CNXT_SHELBY:
737                 case CX231XX_BOARD_CNXT_RDU_250:
738                 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
739                         break;
740                 case CX231XX_BOARD_CNXT_RDE_253S:
741                 case CX231XX_BOARD_CNXT_RDU_253S:
742                 case CX231XX_BOARD_HAUPPAUGE_EXETER:
743                 errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
744                         break;
745                 default:
746                         break;
747                 }
748         }
749 
750         return 0;
751 }
752 EXPORT_SYMBOL_GPL(cx231xx_set_mode);
753 
754 int cx231xx_ep5_bulkout(struct cx231xx *dev, u8 *firmware, u16 size)
755 {
756         int errCode = 0;
757         int actlen, ret = -ENOMEM;
758         u32 *buffer;
759 
760 buffer = kzalloc(4096, GFP_KERNEL);
761         if (buffer == NULL) {
762                 cx231xx_info("out of mem\n");
763                 return -ENOMEM;
764         }
765         memcpy(&buffer[0], firmware, 4096);
766 
767         ret = usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 5),
768                                  buffer, 4096, &actlen, 2000);
769 
770         if (ret)
771                 cx231xx_info("bulk message failed: %d (%d/%d)", ret,
772                                  size, actlen);
773         else {
774                 errCode = actlen != size ? -1 : 0;
775         }
776 kfree(buffer);
777         return 0;
778 }
779 
780 /*****************************************************************
781 *                URB Streaming functions                         *
782 ******************************************************************/
783 
784 /*
785  * IRQ callback, called by URB callback
786  */
787 static void cx231xx_isoc_irq_callback(struct urb *urb)
788 {
789         struct cx231xx_dmaqueue *dma_q = urb->context;
790         struct cx231xx_video_mode *vmode =
791             container_of(dma_q, struct cx231xx_video_mode, vidq);
792         struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
793         int rc, i;
794 
795         switch (urb->status) {
796         case 0:         /* success */
797         case -ETIMEDOUT:        /* NAK */
798                 break;
799         case -ECONNRESET:       /* kill */
800         case -ENOENT:
801         case -ESHUTDOWN:
802                 return;
803         default:                /* error */
804                 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
805                 break;
806         }
807 
808         /* Copy data from URB */
809         spin_lock(&dev->video_mode.slock);
810         rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
811         spin_unlock(&dev->video_mode.slock);
812 
813         /* Reset urb buffers */
814         for (i = 0; i < urb->number_of_packets; i++) {
815                 urb->iso_frame_desc[i].status = 0;
816                 urb->iso_frame_desc[i].actual_length = 0;
817         }
818         urb->status = 0;
819 
820         urb->status = usb_submit_urb(urb, GFP_ATOMIC);
821         if (urb->status) {
822                 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
823                                 urb->status);
824         }
825 }
826 /*****************************************************************
827 *                URB Streaming functions                         *
828 ******************************************************************/
829 
830 /*
831  * IRQ callback, called by URB callback
832  */
833 static void cx231xx_bulk_irq_callback(struct urb *urb)
834 {
835         struct cx231xx_dmaqueue *dma_q = urb->context;
836         struct cx231xx_video_mode *vmode =
837             container_of(dma_q, struct cx231xx_video_mode, vidq);
838         struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
839         int rc;
840 
841         switch (urb->status) {
842         case 0:         /* success */
843         case -ETIMEDOUT:        /* NAK */
844                 break;
845         case -ECONNRESET:       /* kill */
846         case -ENOENT:
847         case -ESHUTDOWN:
848                 return;
849         default:                /* error */
850                 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
851                 break;
852         }
853 
854         /* Copy data from URB */
855         spin_lock(&dev->video_mode.slock);
856         rc = dev->video_mode.bulk_ctl.bulk_copy(dev, urb);
857         spin_unlock(&dev->video_mode.slock);
858 
859         /* Reset urb buffers */
860         urb->status = 0;
861 
862         urb->status = usb_submit_urb(urb, GFP_ATOMIC);
863         if (urb->status) {
864                 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
865                                 urb->status);
866         }
867 }
868 /*
869  * Stop and Deallocate URBs
870  */
871 void cx231xx_uninit_isoc(struct cx231xx *dev)
872 {
873         struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
874         struct urb *urb;
875         int i;
876 
877         cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
878 
879         dev->video_mode.isoc_ctl.nfields = -1;
880         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
881                 urb = dev->video_mode.isoc_ctl.urb[i];
882                 if (urb) {
883                         if (!irqs_disabled())
884                                 usb_kill_urb(urb);
885                         else
886                                 usb_unlink_urb(urb);
887 
888                         if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
889                                 usb_free_coherent(dev->udev,
890                                                   urb->transfer_buffer_length,
891                                                   dev->video_mode.isoc_ctl.
892                                                   transfer_buffer[i],
893                                                   urb->transfer_dma);
894                         }
895                         usb_free_urb(urb);
896                         dev->video_mode.isoc_ctl.urb[i] = NULL;
897                 }
898                 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
899         }
900 
901         kfree(dev->video_mode.isoc_ctl.urb);
902         kfree(dev->video_mode.isoc_ctl.transfer_buffer);
903         kfree(dma_q->p_left_data);
904 
905         dev->video_mode.isoc_ctl.urb = NULL;
906         dev->video_mode.isoc_ctl.transfer_buffer = NULL;
907         dev->video_mode.isoc_ctl.num_bufs = 0;
908         dma_q->p_left_data = NULL;
909 
910         if (dev->mode_tv == 0)
911                 cx231xx_capture_start(dev, 0, Raw_Video);
912         else
913                 cx231xx_capture_start(dev, 0, TS1_serial_mode);
914 
915 
916 }
917 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
918 
919 /*
920  * Stop and Deallocate URBs
921  */
922 void cx231xx_uninit_bulk(struct cx231xx *dev)
923 {
924         struct urb *urb;
925         int i;
926 
927         cx231xx_isocdbg("cx231xx: called cx231xx_uninit_bulk\n");
928 
929         dev->video_mode.bulk_ctl.nfields = -1;
930         for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
931                 urb = dev->video_mode.bulk_ctl.urb[i];
932                 if (urb) {
933                         if (!irqs_disabled())
934                                 usb_kill_urb(urb);
935                         else
936                                 usb_unlink_urb(urb);
937 
938                         if (dev->video_mode.bulk_ctl.transfer_buffer[i]) {
939                                 usb_free_coherent(dev->udev,
940                                                 urb->transfer_buffer_length,
941                                                 dev->video_mode.isoc_ctl.
942                                                 transfer_buffer[i],
943                                                 urb->transfer_dma);
944                         }
945                         usb_free_urb(urb);
946                         dev->video_mode.bulk_ctl.urb[i] = NULL;
947                 }
948                 dev->video_mode.bulk_ctl.transfer_buffer[i] = NULL;
949         }
950 
951         kfree(dev->video_mode.bulk_ctl.urb);
952         kfree(dev->video_mode.bulk_ctl.transfer_buffer);
953 
954         dev->video_mode.bulk_ctl.urb = NULL;
955         dev->video_mode.bulk_ctl.transfer_buffer = NULL;
956         dev->video_mode.bulk_ctl.num_bufs = 0;
957 
958         if (dev->mode_tv == 0)
959                 cx231xx_capture_start(dev, 0, Raw_Video);
960         else
961                 cx231xx_capture_start(dev, 0, TS1_serial_mode);
962 
963 
964 }
965 EXPORT_SYMBOL_GPL(cx231xx_uninit_bulk);
966 
967 /*
968  * Allocate URBs and start IRQ
969  */
970 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
971                       int num_bufs, int max_pkt_size,
972                       int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
973 {
974         struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
975         int i;
976         int sb_size, pipe;
977         struct urb *urb;
978         int j, k;
979         int rc;
980 
981         /* De-allocates all pending stuff */
982         cx231xx_uninit_isoc(dev);
983 
984         dma_q->p_left_data = kzalloc(4096, GFP_KERNEL);
985         if (dma_q->p_left_data == NULL) {
986                 cx231xx_info("out of mem\n");
987                 return -ENOMEM;
988         }
989 
990 
991 
992         dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
993         dev->video_mode.isoc_ctl.num_bufs = num_bufs;
994         dma_q->pos = 0;
995         dma_q->is_partial_line = 0;
996         dma_q->last_sav = 0;
997         dma_q->current_field = -1;
998         dma_q->field1_done = 0;
999         dma_q->lines_per_field = dev->height / 2;
1000         dma_q->bytes_left_in_line = dev->width << 1;
1001         dma_q->lines_completed = 0;
1002         dma_q->mpeg_buffer_done = 0;
1003         dma_q->left_data_count = 0;
1004         dma_q->mpeg_buffer_completed = 0;
1005         dma_q->add_ps_package_head = CX231XX_NEED_ADD_PS_PACKAGE_HEAD;
1006         dma_q->ps_head[0] = 0x00;
1007         dma_q->ps_head[1] = 0x00;
1008         dma_q->ps_head[2] = 0x01;
1009         dma_q->ps_head[3] = 0xBA;
1010         for (i = 0; i < 8; i++)
1011                 dma_q->partial_buf[i] = 0;
1012 
1013         dev->video_mode.isoc_ctl.urb =
1014             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1015         if (!dev->video_mode.isoc_ctl.urb) {
1016                 cx231xx_errdev("cannot alloc memory for usb buffers\n");
1017                 return -ENOMEM;
1018         }
1019 
1020         dev->video_mode.isoc_ctl.transfer_buffer =
1021             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1022         if (!dev->video_mode.isoc_ctl.transfer_buffer) {
1023                 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
1024                 kfree(dev->video_mode.isoc_ctl.urb);
1025                 return -ENOMEM;
1026         }
1027 
1028         dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
1029         dev->video_mode.isoc_ctl.buf = NULL;
1030 
1031         sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
1032 
1033         if (dev->mode_tv == 1)
1034                 dev->video_mode.end_point_addr = 0x81;
1035         else
1036                 dev->video_mode.end_point_addr = 0x84;
1037 
1038 
1039         /* allocate urbs and transfer buffers */
1040         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1041                 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
1042                 if (!urb) {
1043                         cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
1044                         cx231xx_uninit_isoc(dev);
1045                         return -ENOMEM;
1046                 }
1047                 dev->video_mode.isoc_ctl.urb[i] = urb;
1048 
1049                 dev->video_mode.isoc_ctl.transfer_buffer[i] =
1050                     usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1051                                        &urb->transfer_dma);
1052                 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
1053                         cx231xx_err("unable to allocate %i bytes for transfer"
1054                                     " buffer %i%s\n",
1055                                     sb_size, i,
1056                                     in_interrupt() ? " while in int" : "");
1057                         cx231xx_uninit_isoc(dev);
1058                         return -ENOMEM;
1059                 }
1060                 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
1061 
1062                 pipe =
1063                     usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
1064 
1065                 usb_fill_int_urb(urb, dev->udev, pipe,
1066                                  dev->video_mode.isoc_ctl.transfer_buffer[i],
1067                                  sb_size, cx231xx_isoc_irq_callback, dma_q, 1);
1068 
1069                 urb->number_of_packets = max_packets;
1070                 urb->transfer_flags = URB_ISO_ASAP;
1071 
1072                 k = 0;
1073                 for (j = 0; j < max_packets; j++) {
1074                         urb->iso_frame_desc[j].offset = k;
1075                         urb->iso_frame_desc[j].length =
1076                             dev->video_mode.isoc_ctl.max_pkt_size;
1077                         k += dev->video_mode.isoc_ctl.max_pkt_size;
1078                 }
1079         }
1080 
1081         init_waitqueue_head(&dma_q->wq);
1082 
1083         /* submit urbs and enables IRQ */
1084         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
1085                 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
1086                                     GFP_ATOMIC);
1087                 if (rc) {
1088                         cx231xx_err("submit of urb %i failed (error=%i)\n", i,
1089                                     rc);
1090                         cx231xx_uninit_isoc(dev);
1091                         return rc;
1092                 }
1093         }
1094 
1095         if (dev->mode_tv == 0)
1096                 cx231xx_capture_start(dev, 1, Raw_Video);
1097         else
1098                 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1099 
1100         return 0;
1101 }
1102 EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
1103 
1104 /*
1105  * Allocate URBs and start IRQ
1106  */
1107 int cx231xx_init_bulk(struct cx231xx *dev, int max_packets,
1108                       int num_bufs, int max_pkt_size,
1109                       int (*bulk_copy) (struct cx231xx *dev, struct urb *urb))
1110 {
1111         struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
1112         int i;
1113         int sb_size, pipe;
1114         struct urb *urb;
1115         int rc;
1116 
1117         dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1118 
1119         cx231xx_coredbg("Setting Video mux to %d\n", dev->video_input);
1120 
1121         video_mux(dev, dev->video_input);
1122 
1123         /* De-allocates all pending stuff */
1124         cx231xx_uninit_bulk(dev);
1125 
1126         dev->video_mode.bulk_ctl.bulk_copy = bulk_copy;
1127         dev->video_mode.bulk_ctl.num_bufs = num_bufs;
1128         dma_q->pos = 0;
1129         dma_q->is_partial_line = 0;
1130         dma_q->last_sav = 0;
1131         dma_q->current_field = -1;
1132         dma_q->field1_done = 0;
1133         dma_q->lines_per_field = dev->height / 2;
1134         dma_q->bytes_left_in_line = dev->width << 1;
1135         dma_q->lines_completed = 0;
1136         dma_q->mpeg_buffer_done = 0;
1137         dma_q->left_data_count = 0;
1138         dma_q->mpeg_buffer_completed = 0;
1139         dma_q->ps_head[0] = 0x00;
1140         dma_q->ps_head[1] = 0x00;
1141         dma_q->ps_head[2] = 0x01;
1142         dma_q->ps_head[3] = 0xBA;
1143         for (i = 0; i < 8; i++)
1144                 dma_q->partial_buf[i] = 0;
1145 
1146         dev->video_mode.bulk_ctl.urb =
1147             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1148         if (!dev->video_mode.bulk_ctl.urb) {
1149                 cx231xx_errdev("cannot alloc memory for usb buffers\n");
1150                 return -ENOMEM;
1151         }
1152 
1153         dev->video_mode.bulk_ctl.transfer_buffer =
1154             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
1155         if (!dev->video_mode.bulk_ctl.transfer_buffer) {
1156                 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
1157                 kfree(dev->video_mode.bulk_ctl.urb);
1158                 return -ENOMEM;
1159         }
1160 
1161         dev->video_mode.bulk_ctl.max_pkt_size = max_pkt_size;
1162         dev->video_mode.bulk_ctl.buf = NULL;
1163 
1164         sb_size = max_packets * dev->video_mode.bulk_ctl.max_pkt_size;
1165 
1166         if (dev->mode_tv == 1)
1167                 dev->video_mode.end_point_addr = 0x81;
1168         else
1169                 dev->video_mode.end_point_addr = 0x84;
1170 
1171 
1172         /* allocate urbs and transfer buffers */
1173         for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1174                 urb = usb_alloc_urb(0, GFP_KERNEL);
1175                 if (!urb) {
1176                         cx231xx_err("cannot alloc bulk_ctl.urb %i\n", i);
1177                         cx231xx_uninit_bulk(dev);
1178                         return -ENOMEM;
1179                 }
1180                 dev->video_mode.bulk_ctl.urb[i] = urb;
1181                 urb->transfer_flags = 0;
1182 
1183                 dev->video_mode.bulk_ctl.transfer_buffer[i] =
1184                     usb_alloc_coherent(dev->udev, sb_size, GFP_KERNEL,
1185                                      &urb->transfer_dma);
1186                 if (!dev->video_mode.bulk_ctl.transfer_buffer[i]) {
1187                         cx231xx_err("unable to allocate %i bytes for transfer"
1188                                     " buffer %i%s\n",
1189                                     sb_size, i,
1190                                     in_interrupt() ? " while in int" : "");
1191                         cx231xx_uninit_bulk(dev);
1192                         return -ENOMEM;
1193                 }
1194                 memset(dev->video_mode.bulk_ctl.transfer_buffer[i], 0, sb_size);
1195 
1196                 pipe = usb_rcvbulkpipe(dev->udev,
1197                                  dev->video_mode.end_point_addr);
1198                 usb_fill_bulk_urb(urb, dev->udev, pipe,
1199                                   dev->video_mode.bulk_ctl.transfer_buffer[i],
1200                                   sb_size, cx231xx_bulk_irq_callback, dma_q);
1201         }
1202 
1203         init_waitqueue_head(&dma_q->wq);
1204 
1205         /* submit urbs and enables IRQ */
1206         for (i = 0; i < dev->video_mode.bulk_ctl.num_bufs; i++) {
1207                 rc = usb_submit_urb(dev->video_mode.bulk_ctl.urb[i],
1208                                     GFP_ATOMIC);
1209                 if (rc) {
1210                         cx231xx_err("submit of urb %i failed (error=%i)\n", i,
1211                                     rc);
1212                         cx231xx_uninit_bulk(dev);
1213                         return rc;
1214                 }
1215         }
1216 
1217         if (dev->mode_tv == 0)
1218                 cx231xx_capture_start(dev, 1, Raw_Video);
1219         else
1220                 cx231xx_capture_start(dev, 1, TS1_serial_mode);
1221 
1222         return 0;
1223 }
1224 EXPORT_SYMBOL_GPL(cx231xx_init_bulk);
1225 void cx231xx_stop_TS1(struct cx231xx *dev)
1226 {
1227         int status = 0;
1228         u8 val[4] = { 0, 0, 0, 0 };
1229 
1230                         val[0] = 0x00;
1231                         val[1] = 0x03;
1232                         val[2] = 0x00;
1233                         val[3] = 0x00;
1234                         status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1235                                  TS_MODE_REG, val, 4);
1236 
1237                         val[0] = 0x00;
1238                         val[1] = 0x70;
1239                         val[2] = 0x04;
1240                         val[3] = 0x00;
1241                         status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1242                                  TS1_CFG_REG, val, 4);
1243 }
1244 /* EXPORT_SYMBOL_GPL(cx231xx_stop_TS1); */
1245 void cx231xx_start_TS1(struct cx231xx *dev)
1246 {
1247         int status = 0;
1248         u8 val[4] = { 0, 0, 0, 0 };
1249 
1250                         val[0] = 0x03;
1251                         val[1] = 0x03;
1252                         val[2] = 0x00;
1253                         val[3] = 0x00;
1254                         status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1255                                  TS_MODE_REG, val, 4);
1256 
1257                         val[0] = 0x04;
1258                         val[1] = 0xA3;
1259                         val[2] = 0x3B;
1260                         val[3] = 0x00;
1261                         status = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1262                                  TS1_CFG_REG, val, 4);
1263 }
1264 /* EXPORT_SYMBOL_GPL(cx231xx_start_TS1); */
1265 /*****************************************************************
1266 *             Device Init/UnInit functions                       *
1267 ******************************************************************/
1268 int cx231xx_dev_init(struct cx231xx *dev)
1269 {
1270         int errCode = 0;
1271 
1272         /* Initialize I2C bus */
1273 
1274         /* External Master 1 Bus */
1275         dev->i2c_bus[0].nr = 0;
1276         dev->i2c_bus[0].dev = dev;
1277         dev->i2c_bus[0].i2c_period = I2C_SPEED_100K;    /* 100 KHz */
1278         dev->i2c_bus[0].i2c_nostop = 0;
1279         dev->i2c_bus[0].i2c_reserve = 0;
1280 
1281         /* External Master 2 Bus */
1282         dev->i2c_bus[1].nr = 1;
1283         dev->i2c_bus[1].dev = dev;
1284         dev->i2c_bus[1].i2c_period = I2C_SPEED_100K;    /* 100 KHz */
1285         dev->i2c_bus[1].i2c_nostop = 0;
1286         dev->i2c_bus[1].i2c_reserve = 0;
1287 
1288         /* Internal Master 3 Bus */
1289         dev->i2c_bus[2].nr = 2;
1290         dev->i2c_bus[2].dev = dev;
1291         dev->i2c_bus[2].i2c_period = I2C_SPEED_400K;    /* 400kHz */
1292         dev->i2c_bus[2].i2c_nostop = 0;
1293         dev->i2c_bus[2].i2c_reserve = 0;
1294 
1295         /* register I2C buses */
1296         cx231xx_i2c_register(&dev->i2c_bus[0]);
1297         cx231xx_i2c_register(&dev->i2c_bus[1]);
1298         cx231xx_i2c_register(&dev->i2c_bus[2]);
1299 
1300         /* init hardware */
1301         /* Note : with out calling set power mode function,
1302         afe can not be set up correctly */
1303         if (dev->model == CX231XX_BOARD_CNXT_VIDEO_GRABBER ||
1304             dev->model == CX231XX_BOARD_HAUPPAUGE_USBLIVE2) {
1305                 errCode = cx231xx_set_power_mode(dev,
1306                                  POLARIS_AVMODE_ENXTERNAL_AV);
1307                 if (errCode < 0) {
1308                         cx231xx_errdev
1309                         ("%s: Failed to set Power - errCode [%d]!\n",
1310                         __func__, errCode);
1311                         return errCode;
1312                 }
1313         } else {
1314                 errCode = cx231xx_set_power_mode(dev,
1315                                  POLARIS_AVMODE_ANALOGT_TV);
1316                 if (errCode < 0) {
1317                         cx231xx_errdev
1318                         ("%s: Failed to set Power - errCode [%d]!\n",
1319                         __func__, errCode);
1320                         return errCode;
1321                 }
1322         }
1323 
1324         /* reset the Tuner */
1325         if ((dev->model == CX231XX_BOARD_CNXT_CARRAERA) ||
1326                 (dev->model == CX231XX_BOARD_CNXT_RDE_250) ||
1327                 (dev->model == CX231XX_BOARD_CNXT_SHELBY) ||
1328                 (dev->model == CX231XX_BOARD_CNXT_RDU_250))
1329                         cx231xx_gpio_set(dev, dev->board.tuner_gpio);
1330 
1331         /* initialize Colibri block */
1332         errCode = cx231xx_afe_init_super_block(dev, 0x23c);
1333         if (errCode < 0) {
1334                 cx231xx_errdev
1335                     ("%s: cx231xx_afe init super block - errCode [%d]!\n",
1336                      __func__, errCode);
1337                 return errCode;
1338         }
1339         errCode = cx231xx_afe_init_channels(dev);
1340         if (errCode < 0) {
1341                 cx231xx_errdev
1342                     ("%s: cx231xx_afe init channels - errCode [%d]!\n",
1343                      __func__, errCode);
1344                 return errCode;
1345         }
1346 
1347         /* Set DIF in By pass mode */
1348         errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
1349         if (errCode < 0) {
1350                 cx231xx_errdev
1351                     ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
1352                      __func__, errCode);
1353                 return errCode;
1354         }
1355 
1356         /* I2S block related functions */
1357         errCode = cx231xx_i2s_blk_initialize(dev);
1358         if (errCode < 0) {
1359                 cx231xx_errdev
1360                     ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
1361                      __func__, errCode);
1362                 return errCode;
1363         }
1364 
1365         /* init control pins */
1366         errCode = cx231xx_init_ctrl_pin_status(dev);
1367         if (errCode < 0) {
1368                 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
1369                                __func__, errCode);
1370                 return errCode;
1371         }
1372 
1373         /* set AGC mode to Analog */
1374         switch (dev->model) {
1375         case CX231XX_BOARD_CNXT_CARRAERA:
1376         case CX231XX_BOARD_CNXT_RDE_250:
1377         case CX231XX_BOARD_CNXT_SHELBY:
1378         case CX231XX_BOARD_CNXT_RDU_250:
1379         errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
1380                 break;
1381         case CX231XX_BOARD_CNXT_RDE_253S:
1382         case CX231XX_BOARD_CNXT_RDU_253S:
1383         case CX231XX_BOARD_HAUPPAUGE_EXETER:
1384         errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 0);
1385                 break;
1386         default:
1387                 break;
1388         }
1389         if (errCode < 0) {
1390                 cx231xx_errdev
1391                     ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
1392                      __func__, errCode);
1393                 return errCode;
1394         }
1395 
1396         /* set all alternate settings to zero initially */
1397         cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1398         cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1399         cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1400         if (dev->board.has_dvb)
1401                 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
1402 
1403         /* set the I2C master port to 3 on channel 1 */
1404         errCode = cx231xx_enable_i2c_port_3(dev, true);
1405 
1406         return errCode;
1407 }
1408 EXPORT_SYMBOL_GPL(cx231xx_dev_init);
1409 
1410 void cx231xx_dev_uninit(struct cx231xx *dev)
1411 {
1412         /* Un Initialize I2C bus */
1413         cx231xx_i2c_unregister(&dev->i2c_bus[2]);
1414         cx231xx_i2c_unregister(&dev->i2c_bus[1]);
1415         cx231xx_i2c_unregister(&dev->i2c_bus[0]);
1416 }
1417 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
1418 
1419 /*****************************************************************
1420 *              G P I O related functions                         *
1421 ******************************************************************/
1422 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 *gpio_val,
1423                           u8 len, u8 request, u8 direction)
1424 {
1425         int status = 0;
1426         struct VENDOR_REQUEST_IN ven_req;
1427 
1428         /* Set wValue */
1429         ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
1430 
1431         /* set request */
1432         if (!request) {
1433                 if (direction)
1434                         ven_req.bRequest = VRT_GET_GPIO;        /* 0x8 gpio */
1435                 else
1436                         ven_req.bRequest = VRT_SET_GPIO;        /* 0x9 gpio */
1437         } else {
1438                 if (direction)
1439                         ven_req.bRequest = VRT_GET_GPIE;        /* 0xa gpie */
1440                 else
1441                         ven_req.bRequest = VRT_SET_GPIE;        /* 0xb gpie */
1442         }
1443 
1444         /* set index value */
1445         ven_req.wIndex = (u16) (gpio_bit & 0xffff);
1446 
1447         /* set wLength value */
1448         ven_req.wLength = len;
1449 
1450         /* set bData value */
1451         ven_req.bData = 0;
1452 
1453         /* set the buffer for read / write */
1454         ven_req.pBuff = gpio_val;
1455 
1456         /* set the direction */
1457         if (direction) {
1458                 ven_req.direction = USB_DIR_IN;
1459                 memset(ven_req.pBuff, 0x00, ven_req.wLength);
1460         } else
1461                 ven_req.direction = USB_DIR_OUT;
1462 
1463 
1464         /* call common vendor command request */
1465         status = cx231xx_send_vendor_cmd(dev, &ven_req);
1466         if (status < 0) {
1467                 cx231xx_info
1468                     ("UsbInterface::sendCommand, failed with status -%d\n",
1469                      status);
1470         }
1471 
1472         return status;
1473 }
1474 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
1475 
1476 /*****************************************************************
1477  *    C O N T R O L - Register R E A D / W R I T E functions     *
1478  *****************************************************************/
1479 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1480 {
1481         u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1482         u32 tmp = 0;
1483         int status = 0;
1484 
1485         status =
1486             cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1487         if (status < 0)
1488                 return status;
1489 
1490         tmp = *((u32 *) value);
1491         tmp |= mode;
1492 
1493         value[0] = (u8) tmp;
1494         value[1] = (u8) (tmp >> 8);
1495         value[2] = (u8) (tmp >> 16);
1496         value[3] = (u8) (tmp >> 24);
1497 
1498         status =
1499             cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1500 
1501         return status;
1502 }
1503 
1504 /*****************************************************************
1505  *            I 2 C Internal C O N T R O L   functions           *
1506  *****************************************************************/
1507 int cx231xx_read_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1508                           u8 saddr_len, u32 *data, u8 data_len, int master)
1509 {
1510         int status = 0;
1511         struct cx231xx_i2c_xfer_data req_data;
1512         u8 value[64] = "";
1513 
1514         if (saddr_len == 0)
1515                 saddr = 0;
1516         else if (saddr_len == 0)
1517                 saddr &= 0xff;
1518 
1519         /* prepare xfer_data struct */
1520         req_data.dev_addr = dev_addr >> 1;
1521         req_data.direction = I2C_M_RD;
1522         req_data.saddr_len = saddr_len;
1523         req_data.saddr_dat = saddr;
1524         req_data.buf_size = data_len;
1525         req_data.p_buffer = (u8 *) value;
1526 
1527         /* usb send command */
1528         if (master == 0)
1529                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1530                                          &req_data);
1531         else if (master == 1)
1532                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1533                                          &req_data);
1534         else if (master == 2)
1535                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1536                                          &req_data);
1537 
1538         if (status >= 0) {
1539                 /* Copy the data read back to main buffer */
1540                 if (data_len == 1)
1541                         *data = value[0];
1542                 else if (data_len == 4)
1543                         *data =
1544                             value[0] | value[1] << 8 | value[2] << 16 | value[3]
1545                             << 24;
1546                 else if (data_len > 4)
1547                         *data = value[saddr];
1548         }
1549 
1550         return status;
1551 }
1552 
1553 int cx231xx_write_i2c_master(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1554                            u8 saddr_len, u32 data, u8 data_len, int master)
1555 {
1556         int status = 0;
1557         u8 value[4] = { 0, 0, 0, 0 };
1558         struct cx231xx_i2c_xfer_data req_data;
1559 
1560         value[0] = (u8) data;
1561         value[1] = (u8) (data >> 8);
1562         value[2] = (u8) (data >> 16);
1563         value[3] = (u8) (data >> 24);
1564 
1565         if (saddr_len == 0)
1566                 saddr = 0;
1567         else if (saddr_len == 0)
1568                 saddr &= 0xff;
1569 
1570         /* prepare xfer_data struct */
1571         req_data.dev_addr = dev_addr >> 1;
1572         req_data.direction = 0;
1573         req_data.saddr_len = saddr_len;
1574         req_data.saddr_dat = saddr;
1575         req_data.buf_size = data_len;
1576         req_data.p_buffer = value;
1577 
1578         /* usb send command */
1579         if (master == 0)
1580                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0],
1581                                  &req_data);
1582         else if (master == 1)
1583                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[1],
1584                                  &req_data);
1585         else if (master == 2)
1586                 status = dev->cx231xx_send_usb_command(&dev->i2c_bus[2],
1587                                  &req_data);
1588 
1589         return status;
1590 }
1591 
1592 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1593                           u8 saddr_len, u32 *data, u8 data_len)
1594 {
1595         int status = 0;
1596         struct cx231xx_i2c_xfer_data req_data;
1597         u8 value[4] = { 0, 0, 0, 0 };
1598 
1599         if (saddr_len == 0)
1600                 saddr = 0;
1601         else if (saddr_len == 0)
1602                 saddr &= 0xff;
1603 
1604         /* prepare xfer_data struct */
1605         req_data.dev_addr = dev_addr >> 1;
1606         req_data.direction = I2C_M_RD;
1607         req_data.saddr_len = saddr_len;
1608         req_data.saddr_dat = saddr;
1609         req_data.buf_size = data_len;
1610         req_data.p_buffer = (u8 *) value;
1611 
1612         /* usb send command */
1613         status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1614 
1615         if (status >= 0) {
1616                 /* Copy the data read back to main buffer */
1617                 if (data_len == 1)
1618                         *data = value[0];
1619                 else
1620                         *data =
1621                             value[0] | value[1] << 8 | value[2] << 16 | value[3]
1622                             << 24;
1623         }
1624 
1625         return status;
1626 }
1627 
1628 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1629                            u8 saddr_len, u32 data, u8 data_len)
1630 {
1631         int status = 0;
1632         u8 value[4] = { 0, 0, 0, 0 };
1633         struct cx231xx_i2c_xfer_data req_data;
1634 
1635         value[0] = (u8) data;
1636         value[1] = (u8) (data >> 8);
1637         value[2] = (u8) (data >> 16);
1638         value[3] = (u8) (data >> 24);
1639 
1640         if (saddr_len == 0)
1641                 saddr = 0;
1642         else if (saddr_len == 0)
1643                 saddr &= 0xff;
1644 
1645         /* prepare xfer_data struct */
1646         req_data.dev_addr = dev_addr >> 1;
1647         req_data.direction = 0;
1648         req_data.saddr_len = saddr_len;
1649         req_data.saddr_dat = saddr;
1650         req_data.buf_size = data_len;
1651         req_data.p_buffer = value;
1652 
1653         /* usb send command */
1654         status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1655 
1656         return status;
1657 }
1658 
1659 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1660                            u16 register_address, u8 bit_start, u8 bit_end,
1661                            u32 value)
1662 {
1663         int status = 0;
1664         u32 tmp;
1665         u32 mask = 0;
1666         int i;
1667 
1668         if (bit_start > (size - 1) || bit_end > (size - 1))
1669                 return -1;
1670 
1671         if (size == 8) {
1672                 status =
1673                     cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1674                                           &tmp, 1);
1675         } else {
1676                 status =
1677                     cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1678                                           &tmp, 4);
1679         }
1680 
1681         if (status < 0)
1682                 return status;
1683 
1684         mask = 1 << bit_end;
1685         for (i = bit_end; i > bit_start && i > 0; i--)
1686                 mask = mask + (1 << (i - 1));
1687 
1688         value <<= bit_start;
1689 
1690         if (size == 8) {
1691                 tmp &= ~mask;
1692                 tmp |= value;
1693                 tmp &= 0xff;
1694                 status =
1695                     cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1696                                            tmp, 1);
1697         } else {
1698                 tmp &= ~mask;
1699                 tmp |= value;
1700                 status =
1701                     cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1702                                            tmp, 4);
1703         }
1704 
1705         return status;
1706 }
1707 
1708 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1709                                         u16 saddr, u32 mask, u32 value)
1710 {
1711         u32 temp;
1712         int status = 0;
1713 
1714         status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1715 
1716         if (status < 0)
1717                 return status;
1718 
1719         temp &= ~mask;
1720         temp |= value;
1721 
1722         status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1723 
1724         return status;
1725 }
1726 
1727 u32 cx231xx_set_field(u32 field_mask, u32 data)
1728 {
1729         u32 temp;
1730 
1731         for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1732                 data <<= 1;
1733 
1734         return data;
1735 }
1736 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us