Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / 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/usb.h>
27 #include <linux/vmalloc.h>
28 #include <media/v4l2-common.h>
29
30 #include "cx231xx.h"
31 #include "cx231xx-reg.h"
32
33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
34
35 static unsigned int core_debug;
36 module_param(core_debug, int, 0644);
37 MODULE_PARM_DESC(core_debug, "enable debug messages [core]");
38
39 #define cx231xx_coredbg(fmt, arg...) do {\
40         if (core_debug) \
41                 printk(KERN_INFO "%s %s :"fmt, \
42                          dev->name, __func__ , ##arg); } while (0)
43
44 static unsigned int reg_debug;
45 module_param(reg_debug, int, 0644);
46 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
47
48 #define cx231xx_regdbg(fmt, arg...) do {\
49         if (reg_debug) \
50                 printk(KERN_INFO "%s %s :"fmt, \
51                          dev->name, __func__ , ##arg); } while (0)
52
53 static int alt = CX231XX_PINOUT;
54 module_param(alt, int, 0644);
55 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
56
57 #define cx231xx_isocdbg(fmt, arg...) do {\
58         if (core_debug) \
59                 printk(KERN_INFO "%s %s :"fmt, \
60                          dev->name, __func__ , ##arg); } while (0)
61
62 /*****************************************************************
63 *             Device control list functions                                      *
64 ******************************************************************/
65
66 static LIST_HEAD(cx231xx_devlist);
67 static DEFINE_MUTEX(cx231xx_devlist_mutex);
68
69 struct cx231xx *cx231xx_get_device(int minor,
70                                    enum v4l2_buf_type *fh_type, int *has_radio)
71 {
72         struct cx231xx *h, *dev = NULL;
73
74         *fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
75         *has_radio = 0;
76
77         mutex_lock(&cx231xx_devlist_mutex);
78         list_for_each_entry(h, &cx231xx_devlist, devlist) {
79                 if (h->vdev->minor == minor)
80                         dev = h;
81                 if (h->vbi_dev->minor == minor) {
82                         dev = h;
83                         *fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
84                 }
85                 if (h->radio_dev && h->radio_dev->minor == minor) {
86                         dev = h;
87                         *has_radio = 1;
88                 }
89         }
90         mutex_unlock(&cx231xx_devlist_mutex);
91
92         return dev;
93 }
94
95 /*
96  * cx231xx_realease_resources()
97  * unregisters the v4l2,i2c and usb devices
98  * called when the device gets disconected or at module unload
99 */
100 void cx231xx_remove_from_devlist(struct cx231xx *dev)
101 {
102         mutex_lock(&cx231xx_devlist_mutex);
103         list_del(&dev->devlist);
104         mutex_unlock(&cx231xx_devlist_mutex);
105 };
106
107 void cx231xx_add_into_devlist(struct cx231xx *dev)
108 {
109         mutex_lock(&cx231xx_devlist_mutex);
110         list_add_tail(&dev->devlist, &cx231xx_devlist);
111         mutex_unlock(&cx231xx_devlist_mutex);
112 };
113
114 static LIST_HEAD(cx231xx_extension_devlist);
115 static DEFINE_MUTEX(cx231xx_extension_devlist_lock);
116
117 int cx231xx_register_extension(struct cx231xx_ops *ops)
118 {
119         struct cx231xx *dev = NULL;
120
121         mutex_lock(&cx231xx_devlist_mutex);
122         mutex_lock(&cx231xx_extension_devlist_lock);
123         list_add_tail(&ops->next, &cx231xx_extension_devlist);
124         list_for_each_entry(dev, &cx231xx_devlist, devlist) {
125                 if (dev)
126                         ops->init(dev);
127         }
128         printk(KERN_INFO DRIVER_NAME ": %s initialized\n", ops->name);
129         mutex_unlock(&cx231xx_extension_devlist_lock);
130         mutex_unlock(&cx231xx_devlist_mutex);
131         return 0;
132 }
133 EXPORT_SYMBOL(cx231xx_register_extension);
134
135 void cx231xx_unregister_extension(struct cx231xx_ops *ops)
136 {
137         struct cx231xx *dev = NULL;
138
139         mutex_lock(&cx231xx_devlist_mutex);
140         list_for_each_entry(dev, &cx231xx_devlist, devlist) {
141                 if (dev)
142                         ops->fini(dev);
143         }
144
145         mutex_lock(&cx231xx_extension_devlist_lock);
146         printk(KERN_INFO DRIVER_NAME ": %s removed\n", ops->name);
147         list_del(&ops->next);
148         mutex_unlock(&cx231xx_extension_devlist_lock);
149         mutex_unlock(&cx231xx_devlist_mutex);
150 }
151 EXPORT_SYMBOL(cx231xx_unregister_extension);
152
153 void cx231xx_init_extension(struct cx231xx *dev)
154 {
155         struct cx231xx_ops *ops = NULL;
156
157         mutex_lock(&cx231xx_extension_devlist_lock);
158         if (!list_empty(&cx231xx_extension_devlist)) {
159                 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
160                         if (ops->init)
161                                 ops->init(dev);
162                 }
163         }
164         mutex_unlock(&cx231xx_extension_devlist_lock);
165 }
166
167 void cx231xx_close_extension(struct cx231xx *dev)
168 {
169         struct cx231xx_ops *ops = NULL;
170
171         mutex_lock(&cx231xx_extension_devlist_lock);
172         if (!list_empty(&cx231xx_extension_devlist)) {
173                 list_for_each_entry(ops, &cx231xx_extension_devlist, next) {
174                         if (ops->fini)
175                                 ops->fini(dev);
176                 }
177         }
178         mutex_unlock(&cx231xx_extension_devlist_lock);
179 }
180
181 /****************************************************************
182 *               U S B related functions                         *
183 *****************************************************************/
184 int cx231xx_send_usb_command(struct cx231xx_i2c *i2c_bus,
185                              struct cx231xx_i2c_xfer_data *req_data)
186 {
187         int status = 0;
188         struct cx231xx *dev = i2c_bus->dev;
189         struct VENDOR_REQUEST_IN ven_req;
190
191         u8 saddr_len = 0;
192         u8 _i2c_period = 0;
193         u8 _i2c_nostop = 0;
194         u8 _i2c_reserve = 0;
195
196         /* Get the I2C period, nostop and reserve parameters */
197         _i2c_period = i2c_bus->i2c_period;
198         _i2c_nostop = i2c_bus->i2c_nostop;
199         _i2c_reserve = i2c_bus->i2c_reserve;
200
201         saddr_len = req_data->saddr_len;
202
203         /* Set wValue */
204         if (saddr_len == 1)     /* need check saddr_len == 0  */
205                 ven_req.wValue =
206                     req_data->
207                     dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
208                     _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
209         else
210                 ven_req.wValue =
211                     req_data->
212                     dev_addr << 9 | _i2c_period << 4 | saddr_len << 2 |
213                     _i2c_nostop << 1 | I2C_SYNC | _i2c_reserve << 6;
214
215         /* set channel number */
216         if (req_data->direction & I2C_M_RD) {
217                 /* channel number, for read,spec required channel_num +4 */
218                 ven_req.bRequest = i2c_bus->nr + 4;
219         } else
220                 ven_req.bRequest = i2c_bus->nr; /* channel number,  */
221
222         /* set index value */
223         switch (saddr_len) {
224         case 0:
225                 ven_req.wIndex = 0;     /* need check */
226                 break;
227         case 1:
228                 ven_req.wIndex = (req_data->saddr_dat & 0xff);
229                 break;
230         case 2:
231                 ven_req.wIndex = req_data->saddr_dat;
232                 break;
233         }
234
235         /* set wLength value */
236         ven_req.wLength = req_data->buf_size;
237
238         /* set bData value */
239         ven_req.bData = 0;
240
241         /* set the direction */
242         if (req_data->direction) {
243                 ven_req.direction = USB_DIR_IN;
244                 memset(req_data->p_buffer, 0x00, ven_req.wLength);
245         } else
246                 ven_req.direction = USB_DIR_OUT;
247
248         /* set the buffer for read / write */
249         ven_req.pBuff = req_data->p_buffer;
250
251
252         /* call common vendor command request */
253         status = cx231xx_send_vendor_cmd(dev, &ven_req);
254         if (status < 0) {
255                 cx231xx_info
256                     ("UsbInterface::sendCommand, failed with status -%d\n",
257                      status);
258         }
259
260         return status;
261 }
262 EXPORT_SYMBOL_GPL(cx231xx_send_usb_command);
263
264 /*
265  * cx231xx_read_ctrl_reg()
266  * reads data from the usb device specifying bRequest and wValue
267  */
268 int cx231xx_read_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg,
269                           char *buf, int len)
270 {
271         u8 val = 0;
272         int ret;
273         int pipe = usb_rcvctrlpipe(dev->udev, 0);
274
275         if (dev->state & DEV_DISCONNECTED)
276                 return -ENODEV;
277
278         if (len > URB_MAX_CTRL_SIZE)
279                 return -EINVAL;
280
281         switch (len) {
282         case 1:
283                 val = ENABLE_ONE_BYTE;
284                 break;
285         case 2:
286                 val = ENABLE_TWE_BYTE;
287                 break;
288         case 3:
289                 val = ENABLE_THREE_BYTE;
290                 break;
291         case 4:
292                 val = ENABLE_FOUR_BYTE;
293                 break;
294         default:
295                 val = 0xFF;     /* invalid option */
296         }
297
298         if (val == 0xFF)
299                 return -EINVAL;
300
301         if (reg_debug) {
302                 cx231xx_isocdbg("(pipe 0x%08x): "
303                                 "IN:  %02x %02x %02x %02x %02x %02x %02x %02x ",
304                                 pipe,
305                                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
306                                 req, 0, val,
307                                 reg & 0xff, reg >> 8, len & 0xff, len >> 8);
308         }
309
310         mutex_lock(&dev->ctrl_urb_lock);
311         ret = usb_control_msg(dev->udev, pipe, req,
312                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
313                               val, reg, dev->urb_buf, len, HZ);
314         if (ret < 0) {
315                 cx231xx_isocdbg(" failed!\n");
316                 /* mutex_unlock(&dev->ctrl_urb_lock); */
317                 return ret;
318         }
319
320         if (len)
321                 memcpy(buf, dev->urb_buf, len);
322
323         mutex_unlock(&dev->ctrl_urb_lock);
324
325         if (reg_debug) {
326                 int byte;
327
328                 cx231xx_isocdbg("<<<");
329                 for (byte = 0; byte < len; byte++)
330                         cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
331                 cx231xx_isocdbg("\n");
332         }
333
334         return ret;
335 }
336
337 int cx231xx_send_vendor_cmd(struct cx231xx *dev,
338                                 struct VENDOR_REQUEST_IN *ven_req)
339 {
340         int ret;
341         int pipe = 0;
342
343         if (dev->state & DEV_DISCONNECTED)
344                 return -ENODEV;
345
346         if ((ven_req->wLength > URB_MAX_CTRL_SIZE))
347                 return -EINVAL;
348
349         if (ven_req->direction)
350                 pipe = usb_rcvctrlpipe(dev->udev, 0);
351         else
352                 pipe = usb_sndctrlpipe(dev->udev, 0);
353
354         if (reg_debug) {
355                 int byte;
356
357                 cx231xx_isocdbg("(pipe 0x%08x): "
358                                 "OUT: %02x %02x %02x %04x %04x %04x >>>",
359                                 pipe,
360                                 ven_req->
361                                 direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
362                                 ven_req->bRequest, 0, ven_req->wValue,
363                                 ven_req->wIndex, ven_req->wLength);
364
365                 for (byte = 0; byte < ven_req->wLength; byte++)
366                         cx231xx_isocdbg(" %02x",
367                                         (unsigned char)ven_req->pBuff[byte]);
368                 cx231xx_isocdbg("\n");
369         }
370
371         mutex_lock(&dev->ctrl_urb_lock);
372         ret = usb_control_msg(dev->udev, pipe, ven_req->bRequest,
373                               ven_req->
374                               direction | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
375                               ven_req->wValue, ven_req->wIndex, ven_req->pBuff,
376                               ven_req->wLength, HZ);
377         mutex_unlock(&dev->ctrl_urb_lock);
378
379         return ret;
380 }
381
382 /*
383  * cx231xx_write_ctrl_reg()
384  * sends data to the usb device, specifying bRequest
385  */
386 int cx231xx_write_ctrl_reg(struct cx231xx *dev, u8 req, u16 reg, char *buf,
387                            int len)
388 {
389         u8 val = 0;
390         int ret;
391         int pipe = usb_sndctrlpipe(dev->udev, 0);
392
393         if (dev->state & DEV_DISCONNECTED)
394                 return -ENODEV;
395
396         if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
397                 return -EINVAL;
398
399         switch (len) {
400         case 1:
401                 val = ENABLE_ONE_BYTE;
402                 break;
403         case 2:
404                 val = ENABLE_TWE_BYTE;
405                 break;
406         case 3:
407                 val = ENABLE_THREE_BYTE;
408                 break;
409         case 4:
410                 val = ENABLE_FOUR_BYTE;
411                 break;
412         default:
413                 val = 0xFF;     /* invalid option */
414         }
415
416         if (val == 0xFF)
417                 return -EINVAL;
418
419         if (reg_debug) {
420                 int byte;
421
422                 cx231xx_isocdbg("(pipe 0x%08x): "
423                         "OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>>",
424                         pipe,
425                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
426                         req, 0, val, reg & 0xff,
427                         reg >> 8, len & 0xff, len >> 8);
428
429                 for (byte = 0; byte < len; byte++)
430                         cx231xx_isocdbg(" %02x", (unsigned char)buf[byte]);
431                 cx231xx_isocdbg("\n");
432         }
433
434         mutex_lock(&dev->ctrl_urb_lock);
435         memcpy(dev->urb_buf, buf, len);
436         ret = usb_control_msg(dev->udev, pipe, req,
437                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
438                               val, reg, dev->urb_buf, len, HZ);
439         mutex_unlock(&dev->ctrl_urb_lock);
440
441         return ret;
442 }
443
444 /****************************************************************
445 *           USB Alternate Setting functions                     *
446 *****************************************************************/
447
448 int cx231xx_set_video_alternate(struct cx231xx *dev)
449 {
450         int errCode, prev_alt = dev->video_mode.alt;
451         unsigned int min_pkt_size = dev->width * 2 + 4;
452         u32 usb_interface_index = 0;
453
454         /* When image size is bigger than a certain value,
455            the frame size should be increased, otherwise, only
456            green screen will be received.
457          */
458         if (dev->width * 2 * dev->height > 720 * 240 * 2)
459                 min_pkt_size *= 2;
460
461         if (dev->width > 360) {
462                 /* resolutions: 720,704,640 */
463                 dev->video_mode.alt = 3;
464         } else if (dev->width > 180) {
465                 /* resolutions: 360,352,320,240 */
466                 dev->video_mode.alt = 2;
467         } else if (dev->width > 0) {
468                 /* resolutions: 180,176,160,128,88 */
469                 dev->video_mode.alt = 1;
470         } else {
471                 /* Change to alt0 BULK to release USB bandwidth */
472                 dev->video_mode.alt = 0;
473         }
474
475         /* Get the correct video interface Index */
476         usb_interface_index =
477             dev->current_pcb_config.hs_config_info[0].interface_info.
478             video_index + 1;
479
480         if (dev->video_mode.alt != prev_alt) {
481                 cx231xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
482                                 min_pkt_size, dev->video_mode.alt);
483                 dev->video_mode.max_pkt_size =
484                     dev->video_mode.alt_max_pkt_size[dev->video_mode.alt];
485                 cx231xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
486                                 dev->video_mode.alt,
487                                 dev->video_mode.max_pkt_size);
488                 cx231xx_info
489                     (" setting alt %d with wMaxPktSize=%u , Interface = %d\n",
490                      dev->video_mode.alt, dev->video_mode.max_pkt_size,
491                      usb_interface_index);
492                 errCode =
493                     usb_set_interface(dev->udev, usb_interface_index,
494                                       dev->video_mode.alt);
495                 if (errCode < 0) {
496                         cx231xx_errdev
497                             ("cannot change alt number to %d (error=%i)\n",
498                              dev->video_mode.alt, errCode);
499                         return errCode;
500                 }
501         }
502         return 0;
503 }
504
505 int cx231xx_set_alt_setting(struct cx231xx *dev, u8 index, u8 alt)
506 {
507         int status = 0;
508         u32 usb_interface_index = 0;
509         u32 max_pkt_size = 0;
510
511         switch (index) {
512         case INDEX_TS1:
513                 usb_interface_index =
514                     dev->current_pcb_config.hs_config_info[0].interface_info.
515                     ts1_index + 1;
516                 dev->video_mode.alt = alt;
517                 if (dev->ts1_mode.alt_max_pkt_size != NULL)
518                         max_pkt_size = dev->ts1_mode.max_pkt_size =
519                             dev->ts1_mode.alt_max_pkt_size[dev->ts1_mode.alt];
520                 break;
521         case INDEX_TS2:
522                 usb_interface_index =
523                     dev->current_pcb_config.hs_config_info[0].interface_info.
524                     ts2_index + 1;
525                 break;
526         case INDEX_AUDIO:
527                 usb_interface_index =
528                     dev->current_pcb_config.hs_config_info[0].interface_info.
529                     audio_index + 1;
530                 dev->adev.alt = alt;
531                 if (dev->adev.alt_max_pkt_size != NULL)
532                         max_pkt_size = dev->adev.max_pkt_size =
533                             dev->adev.alt_max_pkt_size[dev->adev.alt];
534                 break;
535         case INDEX_VIDEO:
536                 usb_interface_index =
537                     dev->current_pcb_config.hs_config_info[0].interface_info.
538                     video_index + 1;
539                 dev->video_mode.alt = alt;
540                 if (dev->video_mode.alt_max_pkt_size != NULL)
541                         max_pkt_size = dev->video_mode.max_pkt_size =
542                             dev->video_mode.alt_max_pkt_size[dev->video_mode.
543                                                              alt];
544                 break;
545         case INDEX_VANC:
546                 usb_interface_index =
547                     dev->current_pcb_config.hs_config_info[0].interface_info.
548                     vanc_index + 1;
549                 dev->vbi_mode.alt = alt;
550                 if (dev->vbi_mode.alt_max_pkt_size != NULL)
551                         max_pkt_size = dev->vbi_mode.max_pkt_size =
552                             dev->vbi_mode.alt_max_pkt_size[dev->vbi_mode.alt];
553                 break;
554         case INDEX_HANC:
555                 usb_interface_index =
556                     dev->current_pcb_config.hs_config_info[0].interface_info.
557                     hanc_index + 1;
558                 dev->sliced_cc_mode.alt = alt;
559                 if (dev->sliced_cc_mode.alt_max_pkt_size != NULL)
560                         max_pkt_size = dev->sliced_cc_mode.max_pkt_size =
561                             dev->sliced_cc_mode.alt_max_pkt_size[dev->
562                                                                  sliced_cc_mode.
563                                                                  alt];
564                 break;
565         default:
566                 break;
567         }
568
569         if (alt > 0 && max_pkt_size == 0) {
570                 cx231xx_errdev
571                 ("can't change interface %d alt no. to %d: Max. Pkt size = 0\n",
572                 usb_interface_index, alt);
573                 return -1;
574         }
575
576         cx231xx_info
577             (" setting alternate %d with wMaxPacketSize=%u , Interface = %d\n",
578              alt, max_pkt_size, usb_interface_index);
579
580         if (usb_interface_index > 0) {
581                 status = usb_set_interface(dev->udev, usb_interface_index, alt);
582                 if (status < 0) {
583                         cx231xx_errdev
584                         ("can't change interface %d alt no. to %d (err=%i)\n",
585                         usb_interface_index, alt, status);
586                         return status;
587                 }
588         }
589
590         return status;
591 }
592 EXPORT_SYMBOL_GPL(cx231xx_set_alt_setting);
593
594 int cx231xx_gpio_set(struct cx231xx *dev, struct cx231xx_reg_seq *gpio)
595 {
596         int rc = 0;
597
598         if (!gpio)
599                 return rc;
600
601         /* Send GPIO reset sequences specified at board entry */
602         while (gpio->sleep >= 0) {
603                 rc = cx231xx_set_gpio_value(dev, gpio->bit, gpio->val);
604                 if (rc < 0)
605                         return rc;
606
607                 if (gpio->sleep > 0)
608                         msleep(gpio->sleep);
609
610                 gpio++;
611         }
612         return rc;
613 }
614
615 int cx231xx_set_mode(struct cx231xx *dev, enum cx231xx_mode set_mode)
616 {
617         if (dev->mode == set_mode)
618                 return 0;
619
620         if (set_mode == CX231XX_SUSPEND) {
621                 /* Set the chip in power saving mode */
622                 dev->mode = set_mode;
623         }
624
625         /* Resource is locked */
626         if (dev->mode != CX231XX_SUSPEND)
627                 return -EINVAL;
628
629         dev->mode = set_mode;
630
631         if (dev->mode == CX231XX_DIGITAL_MODE)
632                 ;/* Set Digital power mode */
633         else
634                 ;/* Set Analog Power mode */
635
636         return 0;
637 }
638 EXPORT_SYMBOL_GPL(cx231xx_set_mode);
639
640 /*****************************************************************
641 *                URB Streaming functions                         *
642 ******************************************************************/
643
644 /*
645  * IRQ callback, called by URB callback
646  */
647 static void cx231xx_irq_callback(struct urb *urb)
648 {
649         struct cx231xx_dmaqueue *dma_q = urb->context;
650         struct cx231xx_video_mode *vmode =
651             container_of(dma_q, struct cx231xx_video_mode, vidq);
652         struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
653         int rc, i;
654
655         switch (urb->status) {
656         case 0:         /* success */
657         case -ETIMEDOUT:        /* NAK */
658                 break;
659         case -ECONNRESET:       /* kill */
660         case -ENOENT:
661         case -ESHUTDOWN:
662                 return;
663         default:                /* error */
664                 cx231xx_isocdbg("urb completition error %d.\n", urb->status);
665                 break;
666         }
667
668         /* Copy data from URB */
669         spin_lock(&dev->video_mode.slock);
670         rc = dev->video_mode.isoc_ctl.isoc_copy(dev, urb);
671         spin_unlock(&dev->video_mode.slock);
672
673         /* Reset urb buffers */
674         for (i = 0; i < urb->number_of_packets; i++) {
675                 urb->iso_frame_desc[i].status = 0;
676                 urb->iso_frame_desc[i].actual_length = 0;
677         }
678         urb->status = 0;
679
680         urb->status = usb_submit_urb(urb, GFP_ATOMIC);
681         if (urb->status) {
682                 cx231xx_isocdbg("urb resubmit failed (error=%i)\n",
683                                 urb->status);
684         }
685 }
686
687 /*
688  * Stop and Deallocate URBs
689  */
690 void cx231xx_uninit_isoc(struct cx231xx *dev)
691 {
692         struct urb *urb;
693         int i;
694
695         cx231xx_isocdbg("cx231xx: called cx231xx_uninit_isoc\n");
696
697         dev->video_mode.isoc_ctl.nfields = -1;
698         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
699                 urb = dev->video_mode.isoc_ctl.urb[i];
700                 if (urb) {
701                         if (!irqs_disabled())
702                                 usb_kill_urb(urb);
703                         else
704                                 usb_unlink_urb(urb);
705
706                         if (dev->video_mode.isoc_ctl.transfer_buffer[i]) {
707                                 usb_buffer_free(dev->udev,
708                                                 urb->transfer_buffer_length,
709                                                 dev->video_mode.isoc_ctl.
710                                                 transfer_buffer[i],
711                                                 urb->transfer_dma);
712                         }
713                         usb_free_urb(urb);
714                         dev->video_mode.isoc_ctl.urb[i] = NULL;
715                 }
716                 dev->video_mode.isoc_ctl.transfer_buffer[i] = NULL;
717         }
718
719         kfree(dev->video_mode.isoc_ctl.urb);
720         kfree(dev->video_mode.isoc_ctl.transfer_buffer);
721
722         dev->video_mode.isoc_ctl.urb = NULL;
723         dev->video_mode.isoc_ctl.transfer_buffer = NULL;
724         dev->video_mode.isoc_ctl.num_bufs = 0;
725
726         cx231xx_capture_start(dev, 0, Raw_Video);
727 }
728 EXPORT_SYMBOL_GPL(cx231xx_uninit_isoc);
729
730 /*
731  * Allocate URBs and start IRQ
732  */
733 int cx231xx_init_isoc(struct cx231xx *dev, int max_packets,
734                       int num_bufs, int max_pkt_size,
735                       int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
736 {
737         struct cx231xx_dmaqueue *dma_q = &dev->video_mode.vidq;
738         int i;
739         int sb_size, pipe;
740         struct urb *urb;
741         int j, k;
742         int rc;
743
744         cx231xx_isocdbg("cx231xx: called cx231xx_prepare_isoc\n");
745
746         dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
747
748         cx231xx_info("Setting Video mux to %d\n", dev->video_input);
749         video_mux(dev, dev->video_input);
750
751         /* De-allocates all pending stuff */
752         cx231xx_uninit_isoc(dev);
753
754         dev->video_mode.isoc_ctl.isoc_copy = isoc_copy;
755         dev->video_mode.isoc_ctl.num_bufs = num_bufs;
756         dma_q->pos = 0;
757         dma_q->is_partial_line = 0;
758         dma_q->last_sav = 0;
759         dma_q->current_field = -1;
760         dma_q->field1_done = 0;
761         dma_q->lines_per_field = dev->height / 2;
762         dma_q->bytes_left_in_line = dev->width << 1;
763         dma_q->lines_completed = 0;
764         for (i = 0; i < 8; i++)
765                 dma_q->partial_buf[i] = 0;
766
767         dev->video_mode.isoc_ctl.urb =
768             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
769         if (!dev->video_mode.isoc_ctl.urb) {
770                 cx231xx_errdev("cannot alloc memory for usb buffers\n");
771                 return -ENOMEM;
772         }
773
774         dev->video_mode.isoc_ctl.transfer_buffer =
775             kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
776         if (!dev->video_mode.isoc_ctl.transfer_buffer) {
777                 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
778                 kfree(dev->video_mode.isoc_ctl.urb);
779                 return -ENOMEM;
780         }
781
782         dev->video_mode.isoc_ctl.max_pkt_size = max_pkt_size;
783         dev->video_mode.isoc_ctl.buf = NULL;
784
785         sb_size = max_packets * dev->video_mode.isoc_ctl.max_pkt_size;
786
787         /* allocate urbs and transfer buffers */
788         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
789                 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
790                 if (!urb) {
791                         cx231xx_err("cannot alloc isoc_ctl.urb %i\n", i);
792                         cx231xx_uninit_isoc(dev);
793                         return -ENOMEM;
794                 }
795                 dev->video_mode.isoc_ctl.urb[i] = urb;
796
797                 dev->video_mode.isoc_ctl.transfer_buffer[i] =
798                     usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,
799                                      &urb->transfer_dma);
800                 if (!dev->video_mode.isoc_ctl.transfer_buffer[i]) {
801                         cx231xx_err("unable to allocate %i bytes for transfer"
802                                     " buffer %i%s\n",
803                                     sb_size, i,
804                                     in_interrupt() ? " while in int" : "");
805                         cx231xx_uninit_isoc(dev);
806                         return -ENOMEM;
807                 }
808                 memset(dev->video_mode.isoc_ctl.transfer_buffer[i], 0, sb_size);
809
810                 pipe =
811                     usb_rcvisocpipe(dev->udev, dev->video_mode.end_point_addr);
812
813                 usb_fill_int_urb(urb, dev->udev, pipe,
814                                  dev->video_mode.isoc_ctl.transfer_buffer[i],
815                                  sb_size, cx231xx_irq_callback, dma_q, 1);
816
817                 urb->number_of_packets = max_packets;
818                 urb->transfer_flags = URB_ISO_ASAP;
819
820                 k = 0;
821                 for (j = 0; j < max_packets; j++) {
822                         urb->iso_frame_desc[j].offset = k;
823                         urb->iso_frame_desc[j].length =
824                             dev->video_mode.isoc_ctl.max_pkt_size;
825                         k += dev->video_mode.isoc_ctl.max_pkt_size;
826                 }
827         }
828
829         init_waitqueue_head(&dma_q->wq);
830
831         /* submit urbs and enables IRQ */
832         for (i = 0; i < dev->video_mode.isoc_ctl.num_bufs; i++) {
833                 rc = usb_submit_urb(dev->video_mode.isoc_ctl.urb[i],
834                                     GFP_ATOMIC);
835                 if (rc) {
836                         cx231xx_err("submit of urb %i failed (error=%i)\n", i,
837                                     rc);
838                         cx231xx_uninit_isoc(dev);
839                         return rc;
840                 }
841         }
842
843         cx231xx_capture_start(dev, 1, Raw_Video);
844
845         return 0;
846 }
847 EXPORT_SYMBOL_GPL(cx231xx_init_isoc);
848
849 /*****************************************************************
850 *             Device Init/UnInit functions                       *
851 ******************************************************************/
852 int cx231xx_dev_init(struct cx231xx *dev)
853 {
854         int errCode = 0;
855
856         /* Initialize I2C bus */
857
858         /* External Master 1 Bus */
859         dev->i2c_bus[0].nr = 0;
860         dev->i2c_bus[0].dev = dev;
861         dev->i2c_bus[0].i2c_period = I2C_SPEED_1M;      /* 1MHz */
862         dev->i2c_bus[0].i2c_nostop = 0;
863         dev->i2c_bus[0].i2c_reserve = 0;
864
865         /* External Master 2 Bus */
866         dev->i2c_bus[1].nr = 1;
867         dev->i2c_bus[1].dev = dev;
868         dev->i2c_bus[1].i2c_period = I2C_SPEED_1M;      /* 1MHz */
869         dev->i2c_bus[1].i2c_nostop = 0;
870         dev->i2c_bus[1].i2c_reserve = 0;
871
872         /* Internal Master 3 Bus */
873         dev->i2c_bus[2].nr = 2;
874         dev->i2c_bus[2].dev = dev;
875         dev->i2c_bus[2].i2c_period = I2C_SPEED_400K;    /* 400kHz */
876         dev->i2c_bus[2].i2c_nostop = 0;
877         dev->i2c_bus[2].i2c_reserve = 0;
878
879         /* register I2C buses */
880         cx231xx_i2c_register(&dev->i2c_bus[0]);
881         cx231xx_i2c_register(&dev->i2c_bus[1]);
882         cx231xx_i2c_register(&dev->i2c_bus[2]);
883
884         /* init hardware */
885         /* Note : with out calling set power mode function,
886         afe can not be set up correctly */
887         errCode = cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
888         if (errCode < 0) {
889                 cx231xx_errdev
890                     ("%s: Failed to set Power - errCode [%d]!\n",
891                      __func__, errCode);
892                 return errCode;
893         }
894
895         /* initialize Colibri block */
896         errCode = cx231xx_afe_init_super_block(dev, 0x23c);
897         if (errCode < 0) {
898                 cx231xx_errdev
899                     ("%s: cx231xx_afe init super block - errCode [%d]!\n",
900                      __func__, errCode);
901                 return errCode;
902         }
903         errCode = cx231xx_afe_init_channels(dev);
904         if (errCode < 0) {
905                 cx231xx_errdev
906                     ("%s: cx231xx_afe init channels - errCode [%d]!\n",
907                      __func__, errCode);
908                 return errCode;
909         }
910
911         /* Set DIF in By pass mode */
912         errCode = cx231xx_dif_set_standard(dev, DIF_USE_BASEBAND);
913         if (errCode < 0) {
914                 cx231xx_errdev
915                     ("%s: cx231xx_dif set to By pass mode - errCode [%d]!\n",
916                      __func__, errCode);
917                 return errCode;
918         }
919
920         /* I2S block related functions */
921         errCode = cx231xx_i2s_blk_initialize(dev);
922         if (errCode < 0) {
923                 cx231xx_errdev
924                     ("%s: cx231xx_i2s block initialize - errCode [%d]!\n",
925                      __func__, errCode);
926                 return errCode;
927         }
928
929         /* init control pins */
930         errCode = cx231xx_init_ctrl_pin_status(dev);
931         if (errCode < 0) {
932                 cx231xx_errdev("%s: cx231xx_init ctrl pins - errCode [%d]!\n",
933                                __func__, errCode);
934                 return errCode;
935         }
936
937         /* set AGC mode to Analog */
938         errCode = cx231xx_set_agc_analog_digital_mux_select(dev, 1);
939         if (errCode < 0) {
940                 cx231xx_errdev
941                     ("%s: cx231xx_AGC mode to Analog - errCode [%d]!\n",
942                      __func__, errCode);
943                 return errCode;
944         }
945
946         /* set all alternate settings to zero initially */
947         cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
948         cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
949         cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
950         if (dev->board.has_dvb)
951                 cx231xx_set_alt_setting(dev, INDEX_TS1, 0);
952
953         /* set the I2C master port to 3 on channel 1 */
954         errCode = cx231xx_enable_i2c_for_tuner(dev, I2C_3);
955
956         return errCode;
957 }
958 EXPORT_SYMBOL_GPL(cx231xx_dev_init);
959
960 void cx231xx_dev_uninit(struct cx231xx *dev)
961 {
962         /* Un Initialize I2C bus */
963         cx231xx_i2c_unregister(&dev->i2c_bus[2]);
964         cx231xx_i2c_unregister(&dev->i2c_bus[1]);
965         cx231xx_i2c_unregister(&dev->i2c_bus[0]);
966 }
967 EXPORT_SYMBOL_GPL(cx231xx_dev_uninit);
968
969 /*****************************************************************
970 *              G P I O related functions                         *
971 ******************************************************************/
972 int cx231xx_send_gpio_cmd(struct cx231xx *dev, u32 gpio_bit, u8 * gpio_val,
973                           u8 len, u8 request, u8 direction)
974 {
975         int status = 0;
976         struct VENDOR_REQUEST_IN ven_req;
977
978         /* Set wValue */
979         ven_req.wValue = (u16) (gpio_bit >> 16 & 0xffff);
980
981         /* set request */
982         if (!request) {
983                 if (direction)
984                         ven_req.bRequest = VRT_GET_GPIO;        /* 0x8 gpio */
985                 else
986                         ven_req.bRequest = VRT_SET_GPIO;        /* 0x9 gpio */
987         } else {
988                 if (direction)
989                         ven_req.bRequest = VRT_GET_GPIE;        /* 0xa gpie */
990                 else
991                         ven_req.bRequest = VRT_SET_GPIE;        /* 0xb gpie */
992         }
993
994         /* set index value */
995         ven_req.wIndex = (u16) (gpio_bit & 0xffff);
996
997         /* set wLength value */
998         ven_req.wLength = len;
999
1000         /* set bData value */
1001         ven_req.bData = 0;
1002
1003         /* set the buffer for read / write */
1004         ven_req.pBuff = gpio_val;
1005
1006         /* set the direction */
1007         if (direction) {
1008                 ven_req.direction = USB_DIR_IN;
1009                 memset(ven_req.pBuff, 0x00, ven_req.wLength);
1010         } else
1011                 ven_req.direction = USB_DIR_OUT;
1012
1013
1014         /* call common vendor command request */
1015         status = cx231xx_send_vendor_cmd(dev, &ven_req);
1016         if (status < 0) {
1017                 cx231xx_info
1018                     ("UsbInterface::sendCommand, failed with status -%d\n",
1019                      status);
1020         }
1021
1022         return status;
1023 }
1024 EXPORT_SYMBOL_GPL(cx231xx_send_gpio_cmd);
1025
1026 /*****************************************************************
1027  *    C O N T R O L - Register R E A D / W R I T E functions     *
1028  *****************************************************************/
1029 int cx231xx_mode_register(struct cx231xx *dev, u16 address, u32 mode)
1030 {
1031         u8 value[4] = { 0x0, 0x0, 0x0, 0x0 };
1032         u32 tmp = 0;
1033         int status = 0;
1034
1035         status =
1036             cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, address, value, 4);
1037         if (status < 0)
1038                 return status;
1039
1040         tmp = *((u32 *) value);
1041         tmp |= mode;
1042
1043         value[0] = (u8) tmp;
1044         value[1] = (u8) (tmp >> 8);
1045         value[2] = (u8) (tmp >> 16);
1046         value[3] = (u8) (tmp >> 24);
1047
1048         status =
1049             cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, address, value, 4);
1050
1051         return status;
1052 }
1053
1054 /*****************************************************************
1055  *            I 2 C Internal C O N T R O L   functions           *
1056  *****************************************************************/
1057 int cx231xx_read_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1058                           u8 saddr_len, u32 *data, u8 data_len)
1059 {
1060         int status = 0;
1061         struct cx231xx_i2c_xfer_data req_data;
1062         u8 value[4] = { 0, 0, 0, 0 };
1063
1064         if (saddr_len == 0)
1065                 saddr = 0;
1066         else if (saddr_len == 0)
1067                 saddr &= 0xff;
1068
1069         /* prepare xfer_data struct */
1070         req_data.dev_addr = dev_addr >> 1;
1071         req_data.direction = I2C_M_RD;
1072         req_data.saddr_len = saddr_len;
1073         req_data.saddr_dat = saddr;
1074         req_data.buf_size = data_len;
1075         req_data.p_buffer = (u8 *) value;
1076
1077         /* usb send command */
1078         status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1079
1080         if (status >= 0) {
1081                 /* Copy the data read back to main buffer */
1082                 if (data_len == 1)
1083                         *data = value[0];
1084                 else
1085                         *data =
1086                             value[0] | value[1] << 8 | value[2] << 16 | value[3]
1087                             << 24;
1088         }
1089
1090         return status;
1091 }
1092
1093 int cx231xx_write_i2c_data(struct cx231xx *dev, u8 dev_addr, u16 saddr,
1094                            u8 saddr_len, u32 data, u8 data_len)
1095 {
1096         int status = 0;
1097         u8 value[4] = { 0, 0, 0, 0 };
1098         struct cx231xx_i2c_xfer_data req_data;
1099
1100         value[0] = (u8) data;
1101         value[1] = (u8) (data >> 8);
1102         value[2] = (u8) (data >> 16);
1103         value[3] = (u8) (data >> 24);
1104
1105         if (saddr_len == 0)
1106                 saddr = 0;
1107         else if (saddr_len == 0)
1108                 saddr &= 0xff;
1109
1110         /* prepare xfer_data struct */
1111         req_data.dev_addr = dev_addr >> 1;
1112         req_data.direction = 0;
1113         req_data.saddr_len = saddr_len;
1114         req_data.saddr_dat = saddr;
1115         req_data.buf_size = data_len;
1116         req_data.p_buffer = value;
1117
1118         /* usb send command */
1119         status = dev->cx231xx_send_usb_command(&dev->i2c_bus[0], &req_data);
1120
1121         return status;
1122 }
1123
1124 int cx231xx_reg_mask_write(struct cx231xx *dev, u8 dev_addr, u8 size,
1125                            u16 register_address, u8 bit_start, u8 bit_end,
1126                            u32 value)
1127 {
1128         int status = 0;
1129         u32 tmp;
1130         u32 mask = 0;
1131         int i;
1132
1133         if (bit_start > (size - 1) || bit_end > (size - 1))
1134                 return -1;
1135
1136         if (size == 8) {
1137                 status =
1138                     cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1139                                           &tmp, 1);
1140         } else {
1141                 status =
1142                     cx231xx_read_i2c_data(dev, dev_addr, register_address, 2,
1143                                           &tmp, 4);
1144         }
1145
1146         if (status < 0)
1147                 return status;
1148
1149         mask = 1 << bit_end;
1150         for (i = bit_end; i > bit_start && i > 0; i--)
1151                 mask = mask + (1 << (i - 1));
1152
1153         value <<= bit_start;
1154
1155         if (size == 8) {
1156                 tmp &= ~mask;
1157                 tmp |= value;
1158                 tmp &= 0xff;
1159                 status =
1160                     cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1161                                            tmp, 1);
1162         } else {
1163                 tmp &= ~mask;
1164                 tmp |= value;
1165                 status =
1166                     cx231xx_write_i2c_data(dev, dev_addr, register_address, 2,
1167                                            tmp, 4);
1168         }
1169
1170         return status;
1171 }
1172
1173 int cx231xx_read_modify_write_i2c_dword(struct cx231xx *dev, u8 dev_addr,
1174                                         u16 saddr, u32 mask, u32 value)
1175 {
1176         u32 temp;
1177         int status = 0;
1178
1179         status = cx231xx_read_i2c_data(dev, dev_addr, saddr, 2, &temp, 4);
1180
1181         if (status < 0)
1182                 return status;
1183
1184         temp &= ~mask;
1185         temp |= value;
1186
1187         status = cx231xx_write_i2c_data(dev, dev_addr, saddr, 2, temp, 4);
1188
1189         return status;
1190 }
1191
1192 u32 cx231xx_set_field(u32 field_mask, u32 data)
1193 {
1194         u32 temp;
1195
1196         for (temp = field_mask; (temp & 1) == 0; temp >>= 1)
1197                 data <<= 1;
1198
1199         return data;
1200 }