1 /*
2    em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
3 
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 		      Markus Rechberger <mrechberger@gmail.com>
6 		      Mauro Carvalho Chehab <mchehab@infradead.org>
7 		      Sascha Sommer <saschasommer@freenet.de>
8    Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include "em28xx.h"
26 
27 #include <linux/init.h>
28 #include <linux/jiffies.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/usb.h>
33 #include <linux/vmalloc.h>
34 #include <sound/ac97_codec.h>
35 #include <media/v4l2-common.h>
36 
37 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
38 		      "Markus Rechberger <mrechberger@gmail.com>, " \
39 		      "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
40 		      "Sascha Sommer <saschasommer@freenet.de>"
41 
42 MODULE_AUTHOR(DRIVER_AUTHOR);
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(EM28XX_VERSION);
46 
47 /* #define ENABLE_DEBUG_ISOC_FRAMES */
48 
49 static unsigned int core_debug;
50 module_param(core_debug, int, 0644);
51 MODULE_PARM_DESC(core_debug, "enable debug messages [core and isoc]");
52 
53 #define em28xx_coredbg(fmt, arg...) do {				\
54 	if (core_debug)							\
55 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
56 			   "core: %s: " fmt, __func__, ## arg);		\
57 } while (0)
58 
59 static unsigned int reg_debug;
60 module_param(reg_debug, int, 0644);
61 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
62 
63 
64 #define em28xx_regdbg(fmt, arg...) do {				\
65 	if (reg_debug)							\
66 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
67 			   "reg: %s: " fmt, __func__, ## arg);		\
68 } while (0)
69 
70 /* FIXME: don't abuse core_debug */
71 #define em28xx_isocdbg(fmt, arg...) do {				\
72 	if (core_debug)							\
73 		dev_printk(KERN_DEBUG, &dev->intf->dev,			\
74 			   "core: %s: " fmt, __func__, ## arg);		\
75 } while (0)
76 
77 /*
78  * em28xx_read_reg_req()
79  * reads data from the usb device specifying bRequest
80  */
81 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
82 			    char *buf, int len)
83 {
84 	int ret;
85 	struct usb_device *udev = interface_to_usbdev(dev->intf);
86 	int pipe = usb_rcvctrlpipe(udev, 0);
87 
88 	if (dev->disconnected)
89 		return -ENODEV;
90 
91 	if (len > URB_MAX_CTRL_SIZE)
92 		return -EINVAL;
93 
94 	em28xx_regdbg("(pipe 0x%08x): IN:  %02x %02x %02x %02x %02x %02x %02x %02x ",
95 		     pipe, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
96 		     req, 0, 0,
97 		     reg & 0xff, reg >> 8,
98 		     len & 0xff, len >> 8);
99 
100 	mutex_lock(&dev->ctrl_urb_lock);
101 	ret = usb_control_msg(udev, pipe, req,
102 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
103 			      0x0000, reg, dev->urb_buf, len, HZ);
104 	if (ret < 0) {
105 		em28xx_regdbg("(pipe 0x%08x): IN:  %02x %02x %02x %02x %02x %02x %02x %02x  failed\n",
106 			     pipe, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107 			     req, 0, 0,
108 			     reg & 0xff, reg >> 8,
109 			     len & 0xff, len >> 8);
110 		mutex_unlock(&dev->ctrl_urb_lock);
111 		return usb_translate_errors(ret);
112 	}
113 
114 	if (len)
115 		memcpy(buf, dev->urb_buf, len);
116 
117 	mutex_unlock(&dev->ctrl_urb_lock);
118 
119 	em28xx_regdbg("(pipe 0x%08x): IN:  %02x %02x %02x %02x %02x %02x %02x %02x  failed <<< %*ph\n",
120 		     pipe, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
121 		     req, 0, 0,
122 		     reg & 0xff, reg >> 8,
123 		     len & 0xff, len >> 8, len, buf);
124 
125 	return ret;
126 }
127 
128 /*
129  * em28xx_read_reg_req()
130  * reads data from the usb device specifying bRequest
131  */
132 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
133 {
134 	int ret;
135 	u8 val;
136 
137 	ret = em28xx_read_reg_req_len(dev, req, reg, &val, 1);
138 	if (ret < 0)
139 		return ret;
140 
141 	return val;
142 }
143 
144 int em28xx_read_reg(struct em28xx *dev, u16 reg)
145 {
146 	return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
147 }
148 EXPORT_SYMBOL_GPL(em28xx_read_reg);
149 
150 /*
151  * em28xx_write_regs_req()
152  * sends data to the usb device, specifying bRequest
153  */
154 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
155 			  int len)
156 {
157 	int ret;
158 	struct usb_device *udev = interface_to_usbdev(dev->intf);
159 	int pipe = usb_sndctrlpipe(udev, 0);
160 
161 	if (dev->disconnected)
162 		return -ENODEV;
163 
164 	if ((len < 1) || (len > URB_MAX_CTRL_SIZE))
165 		return -EINVAL;
166 
167 	em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph\n",
168 		      pipe,
169 		      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
170 		      req, 0, 0,
171 		      reg & 0xff, reg >> 8,
172 		      len & 0xff, len >> 8, len, buf);
173 
174 	mutex_lock(&dev->ctrl_urb_lock);
175 	memcpy(dev->urb_buf, buf, len);
176 	ret = usb_control_msg(udev, pipe, req,
177 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
178 			      0x0000, reg, dev->urb_buf, len, HZ);
179 	mutex_unlock(&dev->ctrl_urb_lock);
180 
181 	if (ret < 0)
182 		return usb_translate_errors(ret);
183 
184 	if (dev->wait_after_write)
185 		msleep(dev->wait_after_write);
186 
187 	return ret;
188 }
189 
190 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
191 {
192 	return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
193 }
194 EXPORT_SYMBOL_GPL(em28xx_write_regs);
195 
196 /* Write a single register */
197 int em28xx_write_reg(struct em28xx *dev, u16 reg, u8 val)
198 {
199 	return em28xx_write_regs(dev, reg, &val, 1);
200 }
201 EXPORT_SYMBOL_GPL(em28xx_write_reg);
202 
203 /*
204  * em28xx_write_reg_bits()
205  * sets only some bits (specified by bitmask) of a register, by first reading
206  * the actual value
207  */
208 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
209 			  u8 bitmask)
210 {
211 	int oldval;
212 	u8 newval;
213 
214 	oldval = em28xx_read_reg(dev, reg);
215 	if (oldval < 0)
216 		return oldval;
217 
218 	newval = (((u8)oldval) & ~bitmask) | (val & bitmask);
219 
220 	return em28xx_write_regs(dev, reg, &newval, 1);
221 }
222 EXPORT_SYMBOL_GPL(em28xx_write_reg_bits);
223 
224 /*
225  * em28xx_toggle_reg_bits()
226  * toggles/inverts the bits (specified by bitmask) of a register
227  */
228 int em28xx_toggle_reg_bits(struct em28xx *dev, u16 reg, u8 bitmask)
229 {
230 	int oldval;
231 	u8 newval;
232 
233 	oldval = em28xx_read_reg(dev, reg);
234 	if (oldval < 0)
235 		return oldval;
236 
237 	newval = (~oldval & bitmask) | (oldval & ~bitmask);
238 
239 	return em28xx_write_reg(dev, reg, newval);
240 }
241 EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits);
242 
243 /*
244  * em28xx_is_ac97_ready()
245  * Checks if ac97 is ready
246  */
247 static int em28xx_is_ac97_ready(struct em28xx *dev)
248 {
249 	unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT);
250 	int ret;
251 
252 	/* Wait up to 50 ms for AC97 command to complete */
253 	while (time_is_after_jiffies(timeout)) {
254 		ret = em28xx_read_reg(dev, EM28XX_R43_AC97BUSY);
255 		if (ret < 0)
256 			return ret;
257 
258 		if (!(ret & 0x01))
259 			return 0;
260 		msleep(5);
261 	}
262 
263 	dev_warn(&dev->intf->dev,
264 		 "AC97 command still being executed: not handled properly!\n");
265 	return -EBUSY;
266 }
267 
268 /*
269  * em28xx_read_ac97()
270  * write a 16 bit value to the specified AC97 address (LSB first!)
271  */
272 int em28xx_read_ac97(struct em28xx *dev, u8 reg)
273 {
274 	int ret;
275 	u8 addr = (reg & 0x7f) | 0x80;
276 	__le16 val;
277 
278 	ret = em28xx_is_ac97_ready(dev);
279 	if (ret < 0)
280 		return ret;
281 
282 	ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
283 	if (ret < 0)
284 		return ret;
285 
286 	ret = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R40_AC97LSB,
287 					   (u8 *)&val, sizeof(val));
288 
289 	if (ret < 0)
290 		return ret;
291 	return le16_to_cpu(val);
292 }
293 EXPORT_SYMBOL_GPL(em28xx_read_ac97);
294 
295 /*
296  * em28xx_write_ac97()
297  * write a 16 bit value to the specified AC97 address (LSB first!)
298  */
299 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u16 val)
300 {
301 	int ret;
302 	u8 addr = reg & 0x7f;
303 	__le16 value;
304 
305 	value = cpu_to_le16(val);
306 
307 	ret = em28xx_is_ac97_ready(dev);
308 	if (ret < 0)
309 		return ret;
310 
311 	ret = em28xx_write_regs(dev, EM28XX_R40_AC97LSB, (u8 *)&value, 2);
312 	if (ret < 0)
313 		return ret;
314 
315 	ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
316 	if (ret < 0)
317 		return ret;
318 
319 	return 0;
320 }
321 EXPORT_SYMBOL_GPL(em28xx_write_ac97);
322 
323 struct em28xx_vol_itable {
324 	enum em28xx_amux mux;
325 	u8		 reg;
326 };
327 
328 static struct em28xx_vol_itable inputs[] = {
329 	{ EM28XX_AMUX_VIDEO,	AC97_VIDEO	},
330 	{ EM28XX_AMUX_LINE_IN,	AC97_LINE	},
331 	{ EM28XX_AMUX_PHONE,	AC97_PHONE	},
332 	{ EM28XX_AMUX_MIC,	AC97_MIC	},
333 	{ EM28XX_AMUX_CD,	AC97_CD		},
334 	{ EM28XX_AMUX_AUX,	AC97_AUX	},
335 	{ EM28XX_AMUX_PCM_OUT,	AC97_PCM	},
336 };
337 
338 static int set_ac97_input(struct em28xx *dev)
339 {
340 	int ret, i;
341 	enum em28xx_amux amux = dev->ctl_ainput;
342 
343 	/* EM28XX_AMUX_VIDEO2 is a special case used to indicate that
344 	   em28xx should point to LINE IN, while AC97 should use VIDEO
345 	 */
346 	if (amux == EM28XX_AMUX_VIDEO2)
347 		amux = EM28XX_AMUX_VIDEO;
348 
349 	/* Mute all entres but the one that were selected */
350 	for (i = 0; i < ARRAY_SIZE(inputs); i++) {
351 		if (amux == inputs[i].mux)
352 			ret = em28xx_write_ac97(dev, inputs[i].reg, 0x0808);
353 		else
354 			ret = em28xx_write_ac97(dev, inputs[i].reg, 0x8000);
355 
356 		if (ret < 0)
357 			dev_warn(&dev->intf->dev,
358 				 "couldn't setup AC97 register %d\n",
359 				 inputs[i].reg);
360 	}
361 	return 0;
362 }
363 
364 static int em28xx_set_audio_source(struct em28xx *dev)
365 {
366 	int ret;
367 	u8 input;
368 
369 	if (dev->board.is_em2800) {
370 		if (dev->ctl_ainput == EM28XX_AMUX_VIDEO)
371 			input = EM2800_AUDIO_SRC_TUNER;
372 		else
373 			input = EM2800_AUDIO_SRC_LINE;
374 
375 		ret = em28xx_write_regs(dev, EM2800_R08_AUDIOSRC, &input, 1);
376 		if (ret < 0)
377 			return ret;
378 	}
379 
380 	if (dev->board.has_msp34xx)
381 		input = EM28XX_AUDIO_SRC_TUNER;
382 	else {
383 		switch (dev->ctl_ainput) {
384 		case EM28XX_AMUX_VIDEO:
385 			input = EM28XX_AUDIO_SRC_TUNER;
386 			break;
387 		default:
388 			input = EM28XX_AUDIO_SRC_LINE;
389 			break;
390 		}
391 	}
392 
393 	if (dev->board.mute_gpio && dev->mute)
394 		em28xx_gpio_set(dev, dev->board.mute_gpio);
395 	else
396 		em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
397 
398 	ret = em28xx_write_reg_bits(dev, EM28XX_R0E_AUDIOSRC, input, 0xc0);
399 	if (ret < 0)
400 		return ret;
401 	msleep(5);
402 
403 	switch (dev->audio_mode.ac97) {
404 	case EM28XX_NO_AC97:
405 		break;
406 	default:
407 		ret = set_ac97_input(dev);
408 	}
409 
410 	return ret;
411 }
412 
413 struct em28xx_vol_otable {
414 	enum em28xx_aout mux;
415 	u8		 reg;
416 };
417 
418 static const struct em28xx_vol_otable outputs[] = {
419 	{ EM28XX_AOUT_MASTER, AC97_MASTER		},
420 	{ EM28XX_AOUT_LINE,   AC97_HEADPHONE		},
421 	{ EM28XX_AOUT_MONO,   AC97_MASTER_MONO		},
422 	{ EM28XX_AOUT_LFE,    AC97_CENTER_LFE_MASTER	},
423 	{ EM28XX_AOUT_SURR,   AC97_SURROUND_MASTER	},
424 };
425 
426 int em28xx_audio_analog_set(struct em28xx *dev)
427 {
428 	int ret, i;
429 	u8 xclk;
430 
431 	if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE)
432 		return 0;
433 
434 	/* It is assumed that all devices use master volume for output.
435 	   It would be possible to use also line output.
436 	 */
437 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
438 		/* Mute all outputs */
439 		for (i = 0; i < ARRAY_SIZE(outputs); i++) {
440 			ret = em28xx_write_ac97(dev, outputs[i].reg, 0x8000);
441 			if (ret < 0)
442 				dev_warn(&dev->intf->dev,
443 					 "couldn't setup AC97 register %d\n",
444 					 outputs[i].reg);
445 		}
446 	}
447 
448 	xclk = dev->board.xclk & 0x7f;
449 	if (!dev->mute)
450 		xclk |= EM28XX_XCLK_AUDIO_UNMUTE;
451 
452 	ret = em28xx_write_reg(dev, EM28XX_R0F_XCLK, xclk);
453 	if (ret < 0)
454 		return ret;
455 	msleep(10);
456 
457 	/* Selects the proper audio input */
458 	ret = em28xx_set_audio_source(dev);
459 
460 	/* Sets volume */
461 	if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
462 		int vol;
463 
464 		em28xx_write_ac97(dev, AC97_POWERDOWN, 0x4200);
465 		em28xx_write_ac97(dev, AC97_EXTENDED_STATUS, 0x0031);
466 		em28xx_write_ac97(dev, AC97_PCM_LR_ADC_RATE, 0xbb80);
467 
468 		/* LSB: left channel - both channels with the same level */
469 		vol = (0x1f - dev->volume) | ((0x1f - dev->volume) << 8);
470 
471 		/* Mute device, if needed */
472 		if (dev->mute)
473 			vol |= 0x8000;
474 
475 		/* Sets volume */
476 		for (i = 0; i < ARRAY_SIZE(outputs); i++) {
477 			if (dev->ctl_aoutput & outputs[i].mux)
478 				ret = em28xx_write_ac97(dev, outputs[i].reg,
479 							vol);
480 			if (ret < 0)
481 				dev_warn(&dev->intf->dev,
482 					 "couldn't setup AC97 register %d\n",
483 					 outputs[i].reg);
484 		}
485 
486 		if (dev->ctl_aoutput & EM28XX_AOUT_PCM_IN) {
487 			int sel = ac97_return_record_select(dev->ctl_aoutput);
488 
489 			/* Use the same input for both left and right
490 			   channels */
491 			sel |= (sel << 8);
492 
493 			em28xx_write_ac97(dev, AC97_REC_SEL, sel);
494 		}
495 	}
496 
497 	return ret;
498 }
499 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
500 
501 int em28xx_audio_setup(struct em28xx *dev)
502 {
503 	int vid1, vid2, feat, cfg;
504 	u32 vid = 0;
505 	u8 i2s_samplerates;
506 
507 	if (dev->chip_id == CHIP_ID_EM2870 ||
508 	    dev->chip_id == CHIP_ID_EM2874 ||
509 	    dev->chip_id == CHIP_ID_EM28174 ||
510 	    dev->chip_id == CHIP_ID_EM28178) {
511 		/* Digital only device - don't load any alsa module */
512 		dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
513 		dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
514 		return 0;
515 	}
516 
517 	/* See how this device is configured */
518 	cfg = em28xx_read_reg(dev, EM28XX_R00_CHIPCFG);
519 	dev_info(&dev->intf->dev, "Config register raw data: 0x%02x\n", cfg);
520 	if (cfg < 0) { /* Register read error */
521 		/* Be conservative */
522 		dev->int_audio_type = EM28XX_INT_AUDIO_AC97;
523 	} else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) == 0x00) {
524 		/* The device doesn't have vendor audio at all */
525 		dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
526 		dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
527 		return 0;
528 	} else if ((cfg & EM28XX_CHIPCFG_AUDIOMASK) != EM28XX_CHIPCFG_AC97) {
529 		dev->int_audio_type = EM28XX_INT_AUDIO_I2S;
530 		if (dev->chip_id < CHIP_ID_EM2860 &&
531 		    (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
532 		    EM2820_CHIPCFG_I2S_1_SAMPRATE)
533 			i2s_samplerates = 1;
534 		else if (dev->chip_id >= CHIP_ID_EM2860 &&
535 			 (cfg & EM28XX_CHIPCFG_AUDIOMASK) ==
536 			 EM2860_CHIPCFG_I2S_5_SAMPRATES)
537 			i2s_samplerates = 5;
538 		else
539 			i2s_samplerates = 3;
540 		dev_info(&dev->intf->dev, "I2S Audio (%d sample rate(s))\n",
541 			i2s_samplerates);
542 		/* Skip the code that does AC97 vendor detection */
543 		dev->audio_mode.ac97 = EM28XX_NO_AC97;
544 		goto init_audio;
545 	} else {
546 		dev->int_audio_type = EM28XX_INT_AUDIO_AC97;
547 	}
548 
549 	dev->audio_mode.ac97 = EM28XX_AC97_OTHER;
550 
551 	vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);
552 	if (vid1 < 0) {
553 		/*
554 		 * Device likely doesn't support AC97
555 		 * Note: (some) em2800 devices without eeprom reports 0x91 on
556 		 *	 CHIPCFG register, even not having an AC97 chip
557 		 */
558 		dev_warn(&dev->intf->dev,
559 			 "AC97 chip type couldn't be determined\n");
560 		dev->audio_mode.ac97 = EM28XX_NO_AC97;
561 		if (dev->usb_audio_type == EM28XX_USB_AUDIO_VENDOR)
562 			dev->usb_audio_type = EM28XX_USB_AUDIO_NONE;
563 		dev->int_audio_type = EM28XX_INT_AUDIO_NONE;
564 		goto init_audio;
565 	}
566 
567 	vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);
568 	if (vid2 < 0)
569 		goto init_audio;
570 
571 	vid = vid1 << 16 | vid2;
572 	dev_warn(&dev->intf->dev, "AC97 vendor ID = 0x%08x\n", vid);
573 
574 	feat = em28xx_read_ac97(dev, AC97_RESET);
575 	if (feat < 0)
576 		goto init_audio;
577 
578 	dev_warn(&dev->intf->dev, "AC97 features = 0x%04x\n", feat);
579 
580 	/* Try to identify what audio processor we have */
581 	if (((vid == 0xffffffff) || (vid == 0x83847650)) && (feat == 0x6a90))
582 		dev->audio_mode.ac97 = EM28XX_AC97_EM202;
583 	else if ((vid >> 8) == 0x838476)
584 		dev->audio_mode.ac97 = EM28XX_AC97_SIGMATEL;
585 
586 init_audio:
587 	/* Reports detected AC97 processor */
588 	switch (dev->audio_mode.ac97) {
589 	case EM28XX_NO_AC97:
590 		dev_info(&dev->intf->dev, "No AC97 audio processor\n");
591 		break;
592 	case EM28XX_AC97_EM202:
593 		dev_info(&dev->intf->dev,
594 			 "Empia 202 AC97 audio processor detected\n");
595 		break;
596 	case EM28XX_AC97_SIGMATEL:
597 		dev_info(&dev->intf->dev,
598 			 "Sigmatel audio processor detected (stac 97%02x)\n",
599 			 vid & 0xff);
600 		break;
601 	case EM28XX_AC97_OTHER:
602 		dev_warn(&dev->intf->dev,
603 			 "Unknown AC97 audio processor detected!\n");
604 		break;
605 	default:
606 		break;
607 	}
608 
609 	return em28xx_audio_analog_set(dev);
610 }
611 EXPORT_SYMBOL_GPL(em28xx_audio_setup);
612 
613 const struct em28xx_led *em28xx_find_led(struct em28xx *dev,
614 					 enum em28xx_led_role role)
615 {
616 	if (dev->board.leds) {
617 		u8 k = 0;
618 
619 		while (dev->board.leds[k].role >= 0 &&
620 		       dev->board.leds[k].role < EM28XX_NUM_LED_ROLES) {
621 			if (dev->board.leds[k].role == role)
622 				return &dev->board.leds[k];
623 			k++;
624 		}
625 	}
626 	return NULL;
627 }
628 EXPORT_SYMBOL_GPL(em28xx_find_led);
629 
630 int em28xx_capture_start(struct em28xx *dev, int start)
631 {
632 	int rc;
633 	const struct em28xx_led *led = NULL;
634 
635 	if (dev->chip_id == CHIP_ID_EM2874 ||
636 	    dev->chip_id == CHIP_ID_EM2884 ||
637 	    dev->chip_id == CHIP_ID_EM28174 ||
638 	    dev->chip_id == CHIP_ID_EM28178) {
639 		/* The Transport Stream Enable Register moved in em2874 */
640 		rc = em28xx_write_reg_bits(dev, EM2874_R5F_TS_ENABLE,
641 					   start ?
642 					       EM2874_TS1_CAPTURE_ENABLE : 0x00,
643 					   EM2874_TS1_CAPTURE_ENABLE);
644 	} else {
645 		/* FIXME: which is the best order? */
646 		/* video registers are sampled by VREF */
647 		rc = em28xx_write_reg_bits(dev, EM28XX_R0C_USBSUSP,
648 					   start ? 0x10 : 0x00, 0x10);
649 		if (rc < 0)
650 			return rc;
651 
652 		if (start) {
653 			if (dev->board.is_webcam)
654 				rc = em28xx_write_reg(dev, 0x13, 0x0c);
655 
656 			/* Enable video capture */
657 			rc = em28xx_write_reg(dev, 0x48, 0x00);
658 			if (rc < 0)
659 				return rc;
660 
661 			if (dev->mode == EM28XX_ANALOG_MODE)
662 				rc = em28xx_write_reg(dev,
663 						      EM28XX_R12_VINENABLE,
664 						      0x67);
665 			else
666 				rc = em28xx_write_reg(dev,
667 						      EM28XX_R12_VINENABLE,
668 						      0x37);
669 			if (rc < 0)
670 				return rc;
671 
672 			msleep(6);
673 		} else {
674 			/* disable video capture */
675 			rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);
676 		}
677 	}
678 
679 	if (dev->mode == EM28XX_ANALOG_MODE)
680 		led = em28xx_find_led(dev, EM28XX_LED_ANALOG_CAPTURING);
681 	else
682 		led = em28xx_find_led(dev, EM28XX_LED_DIGITAL_CAPTURING);
683 
684 	if (led)
685 		em28xx_write_reg_bits(dev, led->gpio_reg,
686 				      (!start ^ led->inverted) ?
687 				      ~led->gpio_mask : led->gpio_mask,
688 				      led->gpio_mask);
689 
690 	return rc;
691 }
692 
693 int em28xx_gpio_set(struct em28xx *dev, struct em28xx_reg_seq *gpio)
694 {
695 	int rc = 0;
696 
697 	if (!gpio)
698 		return rc;
699 
700 	if (dev->mode != EM28XX_SUSPEND) {
701 		em28xx_write_reg(dev, 0x48, 0x00);
702 		if (dev->mode == EM28XX_ANALOG_MODE)
703 			em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);
704 		else
705 			em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
706 		msleep(6);
707 	}
708 
709 	/* Send GPIO reset sequences specified at board entry */
710 	while (gpio->sleep >= 0) {
711 		if (gpio->reg >= 0) {
712 			rc = em28xx_write_reg_bits(dev,
713 						   gpio->reg,
714 						   gpio->val,
715 						   gpio->mask);
716 			if (rc < 0)
717 				return rc;
718 		}
719 		if (gpio->sleep > 0)
720 			msleep(gpio->sleep);
721 
722 		gpio++;
723 	}
724 	return rc;
725 }
726 EXPORT_SYMBOL_GPL(em28xx_gpio_set);
727 
728 int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)
729 {
730 	if (dev->mode == set_mode)
731 		return 0;
732 
733 	if (set_mode == EM28XX_SUSPEND) {
734 		dev->mode = set_mode;
735 
736 		/* FIXME: add suspend support for ac97 */
737 
738 		return em28xx_gpio_set(dev, dev->board.suspend_gpio);
739 	}
740 
741 	dev->mode = set_mode;
742 
743 	if (dev->mode == EM28XX_DIGITAL_MODE)
744 		return em28xx_gpio_set(dev, dev->board.dvb_gpio);
745 	else
746 		return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
747 }
748 EXPORT_SYMBOL_GPL(em28xx_set_mode);
749 
750 /* ------------------------------------------------------------------
751 	URB control
752    ------------------------------------------------------------------*/
753 
754 /*
755  * URB completion handler for isoc/bulk transfers
756  */
757 static void em28xx_irq_callback(struct urb *urb)
758 {
759 	struct em28xx *dev = urb->context;
760 	int i;
761 
762 	switch (urb->status) {
763 	case 0:             /* success */
764 	case -ETIMEDOUT:    /* NAK */
765 		break;
766 	case -ECONNRESET:   /* kill */
767 	case -ENOENT:
768 	case -ESHUTDOWN:
769 		return;
770 	default:            /* error */
771 		em28xx_isocdbg("urb completition error %d.\n", urb->status);
772 		break;
773 	}
774 
775 	/* Copy data from URB */
776 	spin_lock(&dev->slock);
777 	dev->usb_ctl.urb_data_copy(dev, urb);
778 	spin_unlock(&dev->slock);
779 
780 	/* Reset urb buffers */
781 	for (i = 0; i < urb->number_of_packets; i++) {
782 		/* isoc only (bulk: number_of_packets = 0) */
783 		urb->iso_frame_desc[i].status = 0;
784 		urb->iso_frame_desc[i].actual_length = 0;
785 	}
786 	urb->status = 0;
787 
788 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
789 	if (urb->status) {
790 		em28xx_isocdbg("urb resubmit failed (error=%i)\n",
791 			       urb->status);
792 	}
793 }
794 
795 /*
796  * Stop and Deallocate URBs
797  */
798 void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
799 {
800 	struct urb *urb;
801 	struct em28xx_usb_bufs *usb_bufs;
802 	struct usb_device *udev = interface_to_usbdev(dev->intf);
803 	int i;
804 
805 	em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
806 		       mode);
807 
808 	if (mode == EM28XX_DIGITAL_MODE)
809 		usb_bufs = &dev->usb_ctl.digital_bufs;
810 	else
811 		usb_bufs = &dev->usb_ctl.analog_bufs;
812 
813 	for (i = 0; i < usb_bufs->num_bufs; i++) {
814 		urb = usb_bufs->urb[i];
815 		if (urb) {
816 			if (!irqs_disabled())
817 				usb_kill_urb(urb);
818 			else
819 				usb_unlink_urb(urb);
820 
821 			if (usb_bufs->transfer_buffer[i]) {
822 				usb_free_coherent(udev,
823 						  urb->transfer_buffer_length,
824 						  usb_bufs->transfer_buffer[i],
825 						  urb->transfer_dma);
826 			}
827 			usb_free_urb(urb);
828 			usb_bufs->urb[i] = NULL;
829 		}
830 		usb_bufs->transfer_buffer[i] = NULL;
831 	}
832 
833 	kfree(usb_bufs->urb);
834 	kfree(usb_bufs->transfer_buffer);
835 
836 	usb_bufs->urb = NULL;
837 	usb_bufs->transfer_buffer = NULL;
838 	usb_bufs->num_bufs = 0;
839 
840 	em28xx_capture_start(dev, 0);
841 }
842 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
843 
844 /*
845  * Stop URBs
846  */
847 void em28xx_stop_urbs(struct em28xx *dev)
848 {
849 	int i;
850 	struct urb *urb;
851 	struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
852 
853 	em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
854 
855 	for (i = 0; i < isoc_bufs->num_bufs; i++) {
856 		urb = isoc_bufs->urb[i];
857 		if (urb) {
858 			if (!irqs_disabled())
859 				usb_kill_urb(urb);
860 			else
861 				usb_unlink_urb(urb);
862 		}
863 	}
864 
865 	em28xx_capture_start(dev, 0);
866 }
867 EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
868 
869 /*
870  * Allocate URBs
871  */
872 int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
873 		      int num_bufs, int max_pkt_size, int packet_multiplier)
874 {
875 	struct em28xx_usb_bufs *usb_bufs;
876 	struct urb *urb;
877 	struct usb_device *udev = interface_to_usbdev(dev->intf);
878 	int i;
879 	int sb_size, pipe;
880 	int j, k;
881 
882 	em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode);
883 
884 	/* Check mode and if we have an endpoint for the selected
885 	   transfer type, select buffer				 */
886 	if (mode == EM28XX_DIGITAL_MODE) {
887 		if ((xfer_bulk && !dev->dvb_ep_bulk) ||
888 		    (!xfer_bulk && !dev->dvb_ep_isoc)) {
889 			dev_err(&dev->intf->dev,
890 				"no endpoint for DVB mode and transfer type %d\n",
891 				xfer_bulk > 0);
892 			return -EINVAL;
893 		}
894 		usb_bufs = &dev->usb_ctl.digital_bufs;
895 	} else if (mode == EM28XX_ANALOG_MODE) {
896 		if ((xfer_bulk && !dev->analog_ep_bulk) ||
897 		    (!xfer_bulk && !dev->analog_ep_isoc)) {
898 			dev_err(&dev->intf->dev,
899 				"no endpoint for analog mode and transfer type %d\n",
900 				xfer_bulk > 0);
901 			return -EINVAL;
902 		}
903 		usb_bufs = &dev->usb_ctl.analog_bufs;
904 	} else {
905 		dev_err(&dev->intf->dev, "invalid mode selected\n");
906 		return -EINVAL;
907 	}
908 
909 	/* De-allocates all pending stuff */
910 	em28xx_uninit_usb_xfer(dev, mode);
911 
912 	usb_bufs->num_bufs = num_bufs;
913 
914 	usb_bufs->urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
915 	if (!usb_bufs->urb)
916 		return -ENOMEM;
917 
918 	usb_bufs->transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
919 					     GFP_KERNEL);
920 	if (!usb_bufs->transfer_buffer) {
921 		kfree(usb_bufs->urb);
922 		return -ENOMEM;
923 	}
924 
925 	usb_bufs->max_pkt_size = max_pkt_size;
926 	if (xfer_bulk)
927 		usb_bufs->num_packets = 0;
928 	else
929 		usb_bufs->num_packets = packet_multiplier;
930 	dev->usb_ctl.vid_buf = NULL;
931 	dev->usb_ctl.vbi_buf = NULL;
932 
933 	sb_size = packet_multiplier * usb_bufs->max_pkt_size;
934 
935 	/* allocate urbs and transfer buffers */
936 	for (i = 0; i < usb_bufs->num_bufs; i++) {
937 		urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
938 		if (!urb) {
939 			em28xx_uninit_usb_xfer(dev, mode);
940 			return -ENOMEM;
941 		}
942 		usb_bufs->urb[i] = urb;
943 
944 		usb_bufs->transfer_buffer[i] = usb_alloc_coherent(udev,
945 			sb_size, GFP_KERNEL, &urb->transfer_dma);
946 		if (!usb_bufs->transfer_buffer[i]) {
947 			dev_err(&dev->intf->dev,
948 				"unable to allocate %i bytes for transfer buffer %i%s\n",
949 			       sb_size, i,
950 			       in_interrupt() ? " while in int" : "");
951 			em28xx_uninit_usb_xfer(dev, mode);
952 			return -ENOMEM;
953 		}
954 		memset(usb_bufs->transfer_buffer[i], 0, sb_size);
955 
956 		if (xfer_bulk) { /* bulk */
957 			pipe = usb_rcvbulkpipe(udev,
958 					       mode == EM28XX_ANALOG_MODE ?
959 					       dev->analog_ep_bulk :
960 					       dev->dvb_ep_bulk);
961 			usb_fill_bulk_urb(urb, udev, pipe,
962 					  usb_bufs->transfer_buffer[i], sb_size,
963 					  em28xx_irq_callback, dev);
964 			urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
965 		} else { /* isoc */
966 			pipe = usb_rcvisocpipe(udev,
967 					       mode == EM28XX_ANALOG_MODE ?
968 					       dev->analog_ep_isoc :
969 					       dev->dvb_ep_isoc);
970 			usb_fill_int_urb(urb, udev, pipe,
971 					 usb_bufs->transfer_buffer[i], sb_size,
972 					 em28xx_irq_callback, dev, 1);
973 			urb->transfer_flags = URB_ISO_ASAP |
974 					      URB_NO_TRANSFER_DMA_MAP;
975 			k = 0;
976 			for (j = 0; j < usb_bufs->num_packets; j++) {
977 				urb->iso_frame_desc[j].offset = k;
978 				urb->iso_frame_desc[j].length =
979 							usb_bufs->max_pkt_size;
980 				k += usb_bufs->max_pkt_size;
981 			}
982 		}
983 
984 		urb->number_of_packets = usb_bufs->num_packets;
985 	}
986 
987 	return 0;
988 }
989 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
990 
991 /*
992  * Allocate URBs and start IRQ
993  */
994 int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
995 			 int xfer_bulk, int num_bufs, int max_pkt_size,
996 		    int packet_multiplier,
997 		    int (*urb_data_copy)(struct em28xx *dev, struct urb *urb))
998 {
999 	struct em28xx_dmaqueue *dma_q = &dev->vidq;
1000 	struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
1001 	struct em28xx_usb_bufs *usb_bufs;
1002 	struct usb_device *udev = interface_to_usbdev(dev->intf);
1003 	int i;
1004 	int rc;
1005 	int alloc;
1006 
1007 	em28xx_isocdbg("em28xx: called em28xx_init_usb_xfer in mode %d\n",
1008 		       mode);
1009 
1010 	dev->usb_ctl.urb_data_copy = urb_data_copy;
1011 
1012 	if (mode == EM28XX_DIGITAL_MODE) {
1013 		usb_bufs = &dev->usb_ctl.digital_bufs;
1014 		/* no need to free/alloc usb buffers in digital mode */
1015 		alloc = 0;
1016 	} else {
1017 		usb_bufs = &dev->usb_ctl.analog_bufs;
1018 		alloc = 1;
1019 	}
1020 
1021 	if (alloc) {
1022 		rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1023 				       max_pkt_size, packet_multiplier);
1024 		if (rc)
1025 			return rc;
1026 	}
1027 
1028 	if (xfer_bulk) {
1029 		rc = usb_clear_halt(udev, usb_bufs->urb[0]->pipe);
1030 		if (rc < 0) {
1031 			dev_err(&dev->intf->dev,
1032 				"failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1033 			       rc);
1034 			em28xx_uninit_usb_xfer(dev, mode);
1035 			return rc;
1036 		}
1037 	}
1038 
1039 	init_waitqueue_head(&dma_q->wq);
1040 	init_waitqueue_head(&vbi_dma_q->wq);
1041 
1042 	em28xx_capture_start(dev, 1);
1043 
1044 	/* submit urbs and enables IRQ */
1045 	for (i = 0; i < usb_bufs->num_bufs; i++) {
1046 		rc = usb_submit_urb(usb_bufs->urb[i], GFP_ATOMIC);
1047 		if (rc) {
1048 			dev_err(&dev->intf->dev,
1049 				"submit of urb %i failed (error=%i)\n", i, rc);
1050 			em28xx_uninit_usb_xfer(dev, mode);
1051 			return rc;
1052 		}
1053 	}
1054 
1055 	return 0;
1056 }
1057 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1058 
1059 /*
1060  * Device control list
1061  */
1062 
1063 static LIST_HEAD(em28xx_devlist);
1064 static DEFINE_MUTEX(em28xx_devlist_mutex);
1065 
1066 /*
1067  * Extension interface
1068  */
1069 
1070 static LIST_HEAD(em28xx_extension_devlist);
1071 
1072 int em28xx_register_extension(struct em28xx_ops *ops)
1073 {
1074 	struct em28xx *dev = NULL;
1075 
1076 	mutex_lock(&em28xx_devlist_mutex);
1077 	list_add_tail(&ops->next, &em28xx_extension_devlist);
1078 	list_for_each_entry(dev, &em28xx_devlist, devlist) {
1079 		ops->init(dev);
1080 	}
1081 	mutex_unlock(&em28xx_devlist_mutex);
1082 	pr_info("em28xx: Registered (%s) extension\n", ops->name);
1083 	return 0;
1084 }
1085 EXPORT_SYMBOL(em28xx_register_extension);
1086 
1087 void em28xx_unregister_extension(struct em28xx_ops *ops)
1088 {
1089 	struct em28xx *dev = NULL;
1090 
1091 	mutex_lock(&em28xx_devlist_mutex);
1092 	list_for_each_entry(dev, &em28xx_devlist, devlist) {
1093 		ops->fini(dev);
1094 	}
1095 	list_del(&ops->next);
1096 	mutex_unlock(&em28xx_devlist_mutex);
1097 	pr_info("em28xx: Removed (%s) extension\n", ops->name);
1098 }
1099 EXPORT_SYMBOL(em28xx_unregister_extension);
1100 
1101 void em28xx_init_extension(struct em28xx *dev)
1102 {
1103 	const struct em28xx_ops *ops = NULL;
1104 
1105 	mutex_lock(&em28xx_devlist_mutex);
1106 	list_add_tail(&dev->devlist, &em28xx_devlist);
1107 	list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1108 		if (ops->init)
1109 			ops->init(dev);
1110 	}
1111 	mutex_unlock(&em28xx_devlist_mutex);
1112 }
1113 
1114 void em28xx_close_extension(struct em28xx *dev)
1115 {
1116 	const struct em28xx_ops *ops = NULL;
1117 
1118 	mutex_lock(&em28xx_devlist_mutex);
1119 	list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1120 		if (ops->fini)
1121 			ops->fini(dev);
1122 	}
1123 	list_del(&dev->devlist);
1124 	mutex_unlock(&em28xx_devlist_mutex);
1125 }
1126 
1127 int em28xx_suspend_extension(struct em28xx *dev)
1128 {
1129 	const struct em28xx_ops *ops = NULL;
1130 
1131 	dev_info(&dev->intf->dev, "Suspending extensions\n");
1132 	mutex_lock(&em28xx_devlist_mutex);
1133 	list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1134 		if (ops->suspend)
1135 			ops->suspend(dev);
1136 	}
1137 	mutex_unlock(&em28xx_devlist_mutex);
1138 	return 0;
1139 }
1140 
1141 int em28xx_resume_extension(struct em28xx *dev)
1142 {
1143 	const struct em28xx_ops *ops = NULL;
1144 
1145 	dev_info(&dev->intf->dev, "Resuming extensions\n");
1146 	mutex_lock(&em28xx_devlist_mutex);
1147 	list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1148 		if (ops->resume)
1149 			ops->resume(dev);
1150 	}
1151 	mutex_unlock(&em28xx_devlist_mutex);
1152 	return 0;
1153 }
1154