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