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