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