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