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