perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / em28xx / em28xx-core.c
blob2b8c84a5c9a8e3f82dee3dd348626687c0badcd5
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 //
5 // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 // Markus Rechberger <mrechberger@gmail.com>
7 // Mauro Carvalho Chehab <mchehab@kernel.org>
8 // Sascha Sommer <saschasommer@freenet.de>
9 // Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 #include "em28xx.h"
23 #include <linux/init.h>
24 #include <linux/jiffies.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/usb.h>
29 #include <linux/vmalloc.h>
30 #include <sound/ac97_codec.h>
31 #include <media/v4l2-common.h>
33 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
34 "Markus Rechberger <mrechberger@gmail.com>, " \
35 "Mauro Carvalho Chehab <mchehab@kernel.org>, " \
36 "Sascha Sommer <saschasommer@freenet.de>"
38 MODULE_AUTHOR(DRIVER_AUTHOR);
39 MODULE_DESCRIPTION(DRIVER_DESC);
40 MODULE_LICENSE("GPL v2");
41 MODULE_VERSION(EM28XX_VERSION);
43 /* #define ENABLE_DEBUG_ISOC_FRAMES */
45 static unsigned int core_debug;
46 module_param(core_debug, int, 0644);
47 MODULE_PARM_DESC(core_debug, "enable debug messages [core and isoc]");
49 #define em28xx_coredbg(fmt, arg...) do { \
50 if (core_debug) \
51 dev_printk(KERN_DEBUG, &dev->intf->dev, \
52 "core: %s: " fmt, __func__, ## arg); \
53 } while (0)
55 static unsigned int reg_debug;
56 module_param(reg_debug, int, 0644);
57 MODULE_PARM_DESC(reg_debug, "enable debug messages [URB reg]");
59 #define em28xx_regdbg(fmt, arg...) do { \
60 if (reg_debug) \
61 dev_printk(KERN_DEBUG, &dev->intf->dev, \
62 "reg: %s: " fmt, __func__, ## arg); \
63 } while (0)
65 /* FIXME: don't abuse core_debug */
66 #define em28xx_isocdbg(fmt, arg...) do { \
67 if (core_debug) \
68 dev_printk(KERN_DEBUG, &dev->intf->dev, \
69 "core: %s: " fmt, __func__, ## arg); \
70 } while (0)
73 * em28xx_read_reg_req()
74 * reads data from the usb device specifying bRequest
76 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
77 char *buf, int len)
79 int ret;
80 struct usb_device *udev = interface_to_usbdev(dev->intf);
81 int pipe = usb_rcvctrlpipe(udev, 0);
83 if (dev->disconnected)
84 return -ENODEV;
86 if (len > URB_MAX_CTRL_SIZE)
87 return -EINVAL;
89 mutex_lock(&dev->ctrl_urb_lock);
90 ret = usb_control_msg(udev, pipe, req,
91 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
92 0x0000, reg, dev->urb_buf, len, HZ);
93 if (ret < 0) {
94 em28xx_regdbg("(pipe 0x%08x): IN: %02x %02x %02x %02x %02x %02x %02x %02x failed with error %i\n",
95 pipe,
96 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 req, 0, 0,
98 reg & 0xff, reg >> 8,
99 len & 0xff, len >> 8, ret);
100 mutex_unlock(&dev->ctrl_urb_lock);
101 return usb_translate_errors(ret);
104 if (len)
105 memcpy(buf, dev->urb_buf, len);
107 mutex_unlock(&dev->ctrl_urb_lock);
109 em28xx_regdbg("(pipe 0x%08x): IN: %02x %02x %02x %02x %02x %02x %02x %02x <<< %*ph\n",
110 pipe, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
111 req, 0, 0,
112 reg & 0xff, reg >> 8,
113 len & 0xff, len >> 8, len, buf);
115 return ret;
119 * em28xx_read_reg_req()
120 * reads data from the usb device specifying bRequest
122 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
124 int ret;
125 u8 val;
127 ret = em28xx_read_reg_req_len(dev, req, reg, &val, 1);
128 if (ret < 0)
129 return ret;
131 return val;
134 int em28xx_read_reg(struct em28xx *dev, u16 reg)
136 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
138 EXPORT_SYMBOL_GPL(em28xx_read_reg);
141 * em28xx_write_regs_req()
142 * sends data to the usb device, specifying bRequest
144 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
145 int len)
147 int ret;
148 struct usb_device *udev = interface_to_usbdev(dev->intf);
149 int pipe = usb_sndctrlpipe(udev, 0);
151 if (dev->disconnected)
152 return -ENODEV;
154 if (len < 1 || len > URB_MAX_CTRL_SIZE)
155 return -EINVAL;
157 mutex_lock(&dev->ctrl_urb_lock);
158 memcpy(dev->urb_buf, buf, len);
159 ret = usb_control_msg(udev, pipe, req,
160 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
161 0x0000, reg, dev->urb_buf, len, HZ);
162 mutex_unlock(&dev->ctrl_urb_lock);
164 if (ret < 0) {
165 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph failed with error %i\n",
166 pipe,
167 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
168 req, 0, 0,
169 reg & 0xff, reg >> 8,
170 len & 0xff, len >> 8, len, buf, ret);
171 return usb_translate_errors(ret);
174 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph\n",
175 pipe,
176 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
177 req, 0, 0,
178 reg & 0xff, reg >> 8,
179 len & 0xff, len >> 8, len, buf);
181 if (dev->wait_after_write)
182 msleep(dev->wait_after_write);
184 return ret;
187 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
189 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
191 EXPORT_SYMBOL_GPL(em28xx_write_regs);
193 /* Write a single register */
194 int em28xx_write_reg(struct em28xx *dev, u16 reg, u8 val)
196 return em28xx_write_regs(dev, reg, &val, 1);
198 EXPORT_SYMBOL_GPL(em28xx_write_reg);
201 * em28xx_write_reg_bits()
202 * sets only some bits (specified by bitmask) of a register, by first reading
203 * the actual value
205 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
206 u8 bitmask)
208 int oldval;
209 u8 newval;
211 oldval = em28xx_read_reg(dev, reg);
212 if (oldval < 0)
213 return oldval;
215 newval = (((u8)oldval) & ~bitmask) | (val & bitmask);
217 return em28xx_write_regs(dev, reg, &newval, 1);
219 EXPORT_SYMBOL_GPL(em28xx_write_reg_bits);
222 * em28xx_toggle_reg_bits()
223 * toggles/inverts the bits (specified by bitmask) of a register
225 int em28xx_toggle_reg_bits(struct em28xx *dev, u16 reg, u8 bitmask)
227 int oldval;
228 u8 newval;
230 oldval = em28xx_read_reg(dev, reg);
231 if (oldval < 0)
232 return oldval;
234 newval = (~oldval & bitmask) | (oldval & ~bitmask);
236 return em28xx_write_reg(dev, reg, newval);
238 EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits);
241 * em28xx_is_ac97_ready()
242 * Checks if ac97 is ready
244 static int em28xx_is_ac97_ready(struct em28xx *dev)
246 unsigned long timeout = jiffies + msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT);
247 int ret;
249 /* Wait up to 50 ms for AC97 command to complete */
250 while (time_is_after_jiffies(timeout)) {
251 ret = em28xx_read_reg(dev, EM28XX_R43_AC97BUSY);
252 if (ret < 0)
253 return ret;
255 if (!(ret & 0x01))
256 return 0;
257 msleep(5);
260 dev_warn(&dev->intf->dev,
261 "AC97 command still being executed: not handled properly!\n");
262 return -EBUSY;
266 * em28xx_read_ac97()
267 * write a 16 bit value to the specified AC97 address (LSB first!)
269 int em28xx_read_ac97(struct em28xx *dev, u8 reg)
271 int ret;
272 u8 addr = (reg & 0x7f) | 0x80;
273 __le16 val;
275 ret = em28xx_is_ac97_ready(dev);
276 if (ret < 0)
277 return ret;
279 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
280 if (ret < 0)
281 return ret;
283 ret = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R40_AC97LSB,
284 (u8 *)&val, sizeof(val));
286 if (ret < 0)
287 return ret;
288 return le16_to_cpu(val);
290 EXPORT_SYMBOL_GPL(em28xx_read_ac97);
293 * em28xx_write_ac97()
294 * write a 16 bit value to the specified AC97 address (LSB first!)
296 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u16 val)
298 int ret;
299 u8 addr = reg & 0x7f;
300 __le16 value;
302 value = cpu_to_le16(val);
304 ret = em28xx_is_ac97_ready(dev);
305 if (ret < 0)
306 return ret;
308 ret = em28xx_write_regs(dev, EM28XX_R40_AC97LSB, (u8 *)&value, 2);
309 if (ret < 0)
310 return ret;
312 ret = em28xx_write_regs(dev, EM28XX_R42_AC97ADDR, &addr, 1);
313 if (ret < 0)
314 return ret;
316 return 0;
318 EXPORT_SYMBOL_GPL(em28xx_write_ac97);
320 struct em28xx_vol_itable {
321 enum em28xx_amux mux;
322 u8 reg;
325 static struct em28xx_vol_itable inputs[] = {
326 { EM28XX_AMUX_VIDEO, AC97_VIDEO },
327 { EM28XX_AMUX_LINE_IN, AC97_LINE },
328 { EM28XX_AMUX_PHONE, AC97_PHONE },
329 { EM28XX_AMUX_MIC, AC97_MIC },
330 { EM28XX_AMUX_CD, AC97_CD },
331 { EM28XX_AMUX_AUX, AC97_AUX },
332 { EM28XX_AMUX_PCM_OUT, AC97_PCM },
335 static int set_ac97_input(struct em28xx *dev)
337 int ret, i;
338 enum em28xx_amux amux = dev->ctl_ainput;
341 * EM28XX_AMUX_VIDEO2 is a special case used to indicate that
342 * em28xx should point to LINE IN, while AC97 should use VIDEO
344 if (amux == EM28XX_AMUX_VIDEO2)
345 amux = EM28XX_AMUX_VIDEO;
347 /* Mute all entres but the one that were selected */
348 for (i = 0; i < ARRAY_SIZE(inputs); i++) {
349 if (amux == inputs[i].mux)
350 ret = em28xx_write_ac97(dev, inputs[i].reg, 0x0808);
351 else
352 ret = em28xx_write_ac97(dev, inputs[i].reg, 0x8000);
354 if (ret < 0)
355 dev_warn(&dev->intf->dev,
356 "couldn't setup AC97 register %d\n",
357 inputs[i].reg);
359 return 0;
362 static int em28xx_set_audio_source(struct em28xx *dev)
364 int ret;
365 u8 input;
367 if (dev->board.is_em2800) {
368 if (dev->ctl_ainput == EM28XX_AMUX_VIDEO)
369 input = EM2800_AUDIO_SRC_TUNER;
370 else
371 input = EM2800_AUDIO_SRC_LINE;
373 ret = em28xx_write_regs(dev, EM2800_R08_AUDIOSRC, &input, 1);
374 if (ret < 0)
375 return ret;
378 if (dev->has_msp34xx) {
379 input = EM28XX_AUDIO_SRC_TUNER;
380 } else {
381 switch (dev->ctl_ainput) {
382 case EM28XX_AMUX_VIDEO:
383 input = EM28XX_AUDIO_SRC_TUNER;
384 break;
385 default:
386 input = EM28XX_AUDIO_SRC_LINE;
387 break;
391 if (dev->board.mute_gpio && dev->mute)
392 em28xx_gpio_set(dev, dev->board.mute_gpio);
393 else
394 em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
396 ret = em28xx_write_reg_bits(dev, EM28XX_R0E_AUDIOSRC, input, 0xc0);
397 if (ret < 0)
398 return ret;
399 usleep_range(10000, 11000);
401 switch (dev->audio_mode.ac97) {
402 case EM28XX_NO_AC97:
403 break;
404 default:
405 ret = set_ac97_input(dev);
408 return ret;
411 struct em28xx_vol_otable {
412 enum em28xx_aout mux;
413 u8 reg;
416 static const struct em28xx_vol_otable outputs[] = {
417 { EM28XX_AOUT_MASTER, AC97_MASTER },
418 { EM28XX_AOUT_LINE, AC97_HEADPHONE },
419 { EM28XX_AOUT_MONO, AC97_MASTER_MONO },
420 { EM28XX_AOUT_LFE, AC97_CENTER_LFE_MASTER },
421 { EM28XX_AOUT_SURR, AC97_SURROUND_MASTER },
424 int em28xx_audio_analog_set(struct em28xx *dev)
426 int ret, i;
427 u8 xclk;
429 if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE)
430 return 0;
433 * It is assumed that all devices use master volume for output.
434 * It would be possible to use also line output.
436 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
437 /* Mute all outputs */
438 for (i = 0; i < ARRAY_SIZE(outputs); i++) {
439 ret = em28xx_write_ac97(dev, outputs[i].reg, 0x8000);
440 if (ret < 0)
441 dev_warn(&dev->intf->dev,
442 "couldn't setup AC97 register %d\n",
443 outputs[i].reg);
447 xclk = dev->board.xclk & 0x7f;
448 if (!dev->mute)
449 xclk |= EM28XX_XCLK_AUDIO_UNMUTE;
451 ret = em28xx_write_reg(dev, EM28XX_R0F_XCLK, xclk);
452 if (ret < 0)
453 return ret;
454 usleep_range(10000, 11000);
456 /* Selects the proper audio input */
457 ret = em28xx_set_audio_source(dev);
459 /* Sets volume */
460 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
461 int vol;
463 em28xx_write_ac97(dev, AC97_POWERDOWN, 0x4200);
464 em28xx_write_ac97(dev, AC97_EXTENDED_STATUS, 0x0031);
465 em28xx_write_ac97(dev, AC97_PCM_LR_ADC_RATE, 0xbb80);
467 /* LSB: left channel - both channels with the same level */
468 vol = (0x1f - dev->volume) | ((0x1f - dev->volume) << 8);
470 /* Mute device, if needed */
471 if (dev->mute)
472 vol |= 0x8000;
474 /* Sets volume */
475 for (i = 0; i < ARRAY_SIZE(outputs); i++) {
476 if (dev->ctl_aoutput & outputs[i].mux)
477 ret = em28xx_write_ac97(dev, outputs[i].reg,
478 vol);
479 if (ret < 0)
480 dev_warn(&dev->intf->dev,
481 "couldn't setup AC97 register %d\n",
482 outputs[i].reg);
485 if (dev->ctl_aoutput & EM28XX_AOUT_PCM_IN) {
486 int sel = ac97_return_record_select(dev->ctl_aoutput);
489 * Use the same input for both left and right
490 * channels
492 sel |= (sel << 8);
494 em28xx_write_ac97(dev, AC97_REC_SEL, sel);
498 return ret;
500 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
502 int em28xx_audio_setup(struct em28xx *dev)
504 int vid1, vid2, feat, cfg;
505 u32 vid = 0;
506 u8 i2s_samplerates;
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;
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;
550 dev->audio_mode.ac97 = EM28XX_AC97_OTHER;
552 vid1 = em28xx_read_ac97(dev, AC97_VENDOR_ID1);
553 if (vid1 < 0) {
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
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;
568 vid2 = em28xx_read_ac97(dev, AC97_VENDOR_ID2);
569 if (vid2 < 0)
570 goto init_audio;
572 vid = vid1 << 16 | vid2;
573 dev_warn(&dev->intf->dev, "AC97 vendor ID = 0x%08x\n", vid);
575 feat = em28xx_read_ac97(dev, AC97_RESET);
576 if (feat < 0)
577 goto init_audio;
579 dev_warn(&dev->intf->dev, "AC97 features = 0x%04x\n", feat);
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;
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;
610 return em28xx_audio_analog_set(dev);
612 EXPORT_SYMBOL_GPL(em28xx_audio_setup);
614 const struct em28xx_led *em28xx_find_led(struct em28xx *dev,
615 enum em28xx_led_role role)
617 if (dev->board.leds) {
618 u8 k = 0;
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++;
627 return NULL;
629 EXPORT_SYMBOL_GPL(em28xx_find_led);
631 int em28xx_capture_start(struct em28xx *dev, int start)
633 int rc;
634 const struct em28xx_led *led = NULL;
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 if (dev->dvb_xfer_bulk) {
642 /* Max Tx Size = 188 * 256 = 48128 - LCM(188,512) * 2 */
643 em28xx_write_reg(dev, (dev->ts == PRIMARY_TS) ?
644 EM2874_R5D_TS1_PKT_SIZE :
645 EM2874_R5E_TS2_PKT_SIZE,
646 0xff);
647 } else {
648 /* ISOC Maximum Transfer Size = 188 * 5 */
649 em28xx_write_reg(dev, (dev->ts == PRIMARY_TS) ?
650 EM2874_R5D_TS1_PKT_SIZE :
651 EM2874_R5E_TS2_PKT_SIZE,
652 dev->dvb_max_pkt_size_isoc / 188);
654 if (dev->ts == PRIMARY_TS)
655 rc = em28xx_write_reg_bits(dev,
656 EM2874_R5F_TS_ENABLE,
657 start ? EM2874_TS1_CAPTURE_ENABLE : 0x00,
658 EM2874_TS1_CAPTURE_ENABLE | EM2874_TS1_FILTER_ENABLE | EM2874_TS1_NULL_DISCARD);
659 else
660 rc = em28xx_write_reg_bits(dev,
661 EM2874_R5F_TS_ENABLE,
662 start ? EM2874_TS2_CAPTURE_ENABLE : 0x00,
663 EM2874_TS2_CAPTURE_ENABLE | EM2874_TS2_FILTER_ENABLE | EM2874_TS2_NULL_DISCARD);
664 } else {
665 /* FIXME: which is the best order? */
666 /* video registers are sampled by VREF */
667 rc = em28xx_write_reg_bits(dev, EM28XX_R0C_USBSUSP,
668 start ? 0x10 : 0x00, 0x10);
669 if (rc < 0)
670 return rc;
672 if (start) {
673 if (dev->is_webcam)
674 rc = em28xx_write_reg(dev, 0x13, 0x0c);
676 /* Enable video capture */
677 rc = em28xx_write_reg(dev, 0x48, 0x00);
678 if (rc < 0)
679 return rc;
681 if (dev->mode == EM28XX_ANALOG_MODE)
682 rc = em28xx_write_reg(dev,
683 EM28XX_R12_VINENABLE,
684 0x67);
685 else
686 rc = em28xx_write_reg(dev,
687 EM28XX_R12_VINENABLE,
688 0x37);
689 if (rc < 0)
690 return rc;
692 usleep_range(10000, 11000);
693 } else {
694 /* disable video capture */
695 rc = em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x27);
699 if (dev->mode == EM28XX_ANALOG_MODE)
700 led = em28xx_find_led(dev, EM28XX_LED_ANALOG_CAPTURING);
701 else
702 led = em28xx_find_led(dev, EM28XX_LED_DIGITAL_CAPTURING);
704 if (led)
705 em28xx_write_reg_bits(dev, led->gpio_reg,
706 (!start ^ led->inverted) ?
707 ~led->gpio_mask : led->gpio_mask,
708 led->gpio_mask);
710 return rc;
713 int em28xx_gpio_set(struct em28xx *dev, const struct em28xx_reg_seq *gpio)
715 int rc = 0;
717 if (!gpio)
718 return rc;
720 if (dev->mode != EM28XX_SUSPEND) {
721 em28xx_write_reg(dev, 0x48, 0x00);
722 if (dev->mode == EM28XX_ANALOG_MODE)
723 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);
724 else
725 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
726 usleep_range(10000, 11000);
729 /* Send GPIO reset sequences specified at board entry */
730 while (gpio->sleep >= 0) {
731 if (gpio->reg >= 0) {
732 rc = em28xx_write_reg_bits(dev,
733 gpio->reg,
734 gpio->val,
735 gpio->mask);
736 if (rc < 0)
737 return rc;
739 if (gpio->sleep > 0)
740 msleep(gpio->sleep);
742 gpio++;
744 return rc;
746 EXPORT_SYMBOL_GPL(em28xx_gpio_set);
748 int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)
750 if (dev->mode == set_mode)
751 return 0;
753 if (set_mode == EM28XX_SUSPEND) {
754 dev->mode = set_mode;
756 /* FIXME: add suspend support for ac97 */
758 return em28xx_gpio_set(dev, dev->board.suspend_gpio);
761 dev->mode = set_mode;
763 if (dev->mode == EM28XX_DIGITAL_MODE)
764 return em28xx_gpio_set(dev, dev->board.dvb_gpio);
765 else
766 return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
768 EXPORT_SYMBOL_GPL(em28xx_set_mode);
771 *URB control
775 * URB completion handler for isoc/bulk transfers
777 static void em28xx_irq_callback(struct urb *urb)
779 struct em28xx *dev = urb->context;
780 unsigned long flags;
781 int i;
783 switch (urb->status) {
784 case 0: /* success */
785 case -ETIMEDOUT: /* NAK */
786 break;
787 case -ECONNRESET: /* kill */
788 case -ENOENT:
789 case -ESHUTDOWN:
790 return;
791 default: /* error */
792 em28xx_isocdbg("urb completion error %d.\n", urb->status);
793 break;
796 /* Copy data from URB */
797 spin_lock_irqsave(&dev->slock, flags);
798 dev->usb_ctl.urb_data_copy(dev, urb);
799 spin_unlock_irqrestore(&dev->slock, flags);
801 /* Reset urb buffers */
802 for (i = 0; i < urb->number_of_packets; i++) {
803 /* isoc only (bulk: number_of_packets = 0) */
804 urb->iso_frame_desc[i].status = 0;
805 urb->iso_frame_desc[i].actual_length = 0;
807 urb->status = 0;
809 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
810 if (urb->status) {
811 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
812 urb->status);
817 * Stop and Deallocate URBs
819 void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
821 struct urb *urb;
822 struct em28xx_usb_bufs *usb_bufs;
823 int i;
825 em28xx_isocdbg("called %s in mode %d\n", __func__, mode);
827 if (mode == EM28XX_DIGITAL_MODE)
828 usb_bufs = &dev->usb_ctl.digital_bufs;
829 else
830 usb_bufs = &dev->usb_ctl.analog_bufs;
832 for (i = 0; i < usb_bufs->num_bufs; i++) {
833 urb = usb_bufs->urb[i];
834 if (urb) {
835 if (!irqs_disabled())
836 usb_kill_urb(urb);
837 else
838 usb_unlink_urb(urb);
840 usb_free_urb(urb);
841 usb_bufs->urb[i] = NULL;
845 kfree(usb_bufs->urb);
846 kfree(usb_bufs->buf);
848 usb_bufs->urb = NULL;
849 usb_bufs->buf = NULL;
850 usb_bufs->num_bufs = 0;
852 em28xx_capture_start(dev, 0);
854 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
857 * Stop URBs
859 void em28xx_stop_urbs(struct em28xx *dev)
861 int i;
862 struct urb *urb;
863 struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
865 em28xx_isocdbg("called %s\n", __func__);
867 for (i = 0; i < isoc_bufs->num_bufs; i++) {
868 urb = isoc_bufs->urb[i];
869 if (urb) {
870 if (!irqs_disabled())
871 usb_kill_urb(urb);
872 else
873 usb_unlink_urb(urb);
877 em28xx_capture_start(dev, 0);
879 EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
882 * Allocate URBs
884 int em28xx_alloc_urbs(struct em28xx *dev, enum em28xx_mode mode, int xfer_bulk,
885 int num_bufs, int max_pkt_size, int packet_multiplier)
887 struct em28xx_usb_bufs *usb_bufs;
888 struct urb *urb;
889 struct usb_device *udev = interface_to_usbdev(dev->intf);
890 int i;
891 int sb_size, pipe;
892 int j, k;
894 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__, mode);
897 * Check mode and if we have an endpoint for the selected
898 * transfer type, select buffer
900 if (mode == EM28XX_DIGITAL_MODE) {
901 if ((xfer_bulk && !dev->dvb_ep_bulk) ||
902 (!xfer_bulk && !dev->dvb_ep_isoc)) {
903 dev_err(&dev->intf->dev,
904 "no endpoint for DVB mode and transfer type %d\n",
905 xfer_bulk > 0);
906 return -EINVAL;
908 usb_bufs = &dev->usb_ctl.digital_bufs;
909 } else if (mode == EM28XX_ANALOG_MODE) {
910 if ((xfer_bulk && !dev->analog_ep_bulk) ||
911 (!xfer_bulk && !dev->analog_ep_isoc)) {
912 dev_err(&dev->intf->dev,
913 "no endpoint for analog mode and transfer type %d\n",
914 xfer_bulk > 0);
915 return -EINVAL;
917 usb_bufs = &dev->usb_ctl.analog_bufs;
918 } else {
919 dev_err(&dev->intf->dev, "invalid mode selected\n");
920 return -EINVAL;
923 /* De-allocates all pending stuff */
924 em28xx_uninit_usb_xfer(dev, mode);
926 usb_bufs->num_bufs = num_bufs;
928 usb_bufs->urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
929 if (!usb_bufs->urb)
930 return -ENOMEM;
932 usb_bufs->buf = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
933 if (!usb_bufs->buf) {
934 kfree(usb_bufs->buf);
935 return -ENOMEM;
938 usb_bufs->max_pkt_size = max_pkt_size;
939 if (xfer_bulk)
940 usb_bufs->num_packets = 0;
941 else
942 usb_bufs->num_packets = packet_multiplier;
943 dev->usb_ctl.vid_buf = NULL;
944 dev->usb_ctl.vbi_buf = NULL;
946 sb_size = packet_multiplier * usb_bufs->max_pkt_size;
948 /* allocate urbs and transfer buffers */
949 for (i = 0; i < usb_bufs->num_bufs; i++) {
950 urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
951 if (!urb) {
952 em28xx_uninit_usb_xfer(dev, mode);
953 return -ENOMEM;
955 usb_bufs->urb[i] = urb;
957 usb_bufs->buf[i] = kzalloc(sb_size, GFP_KERNEL);
958 if (!usb_bufs->buf[i]) {
959 em28xx_uninit_usb_xfer(dev, mode);
961 for (i--; i >= 0; i--)
962 kfree(usb_bufs->buf[i]);
964 kfree(usb_bufs->buf);
965 usb_bufs->buf = NULL;
967 return -ENOMEM;
970 urb->transfer_flags = URB_FREE_BUFFER;
972 if (xfer_bulk) { /* bulk */
973 pipe = usb_rcvbulkpipe(udev,
974 mode == EM28XX_ANALOG_MODE ?
975 dev->analog_ep_bulk :
976 dev->dvb_ep_bulk);
977 usb_fill_bulk_urb(urb, udev, pipe, usb_bufs->buf[i],
978 sb_size, em28xx_irq_callback, dev);
979 } else { /* isoc */
980 pipe = usb_rcvisocpipe(udev,
981 mode == EM28XX_ANALOG_MODE ?
982 dev->analog_ep_isoc :
983 dev->dvb_ep_isoc);
984 usb_fill_int_urb(urb, udev, pipe, usb_bufs->buf[i],
985 sb_size, em28xx_irq_callback, dev, 1);
986 urb->transfer_flags |= URB_ISO_ASAP;
987 k = 0;
988 for (j = 0; j < usb_bufs->num_packets; j++) {
989 urb->iso_frame_desc[j].offset = k;
990 urb->iso_frame_desc[j].length =
991 usb_bufs->max_pkt_size;
992 k += usb_bufs->max_pkt_size;
996 urb->number_of_packets = usb_bufs->num_packets;
999 return 0;
1001 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
1004 * Allocate URBs and start IRQ
1006 int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
1007 int xfer_bulk, int num_bufs, int max_pkt_size,
1008 int packet_multiplier,
1009 int (*urb_data_copy)(struct em28xx *dev, struct urb *urb))
1011 struct em28xx_dmaqueue *dma_q = &dev->vidq;
1012 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
1013 struct em28xx_usb_bufs *usb_bufs;
1014 struct usb_device *udev = interface_to_usbdev(dev->intf);
1015 int i;
1016 int rc;
1017 int alloc;
1019 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__, mode);
1021 dev->usb_ctl.urb_data_copy = urb_data_copy;
1023 if (mode == EM28XX_DIGITAL_MODE) {
1024 usb_bufs = &dev->usb_ctl.digital_bufs;
1025 /* no need to free/alloc usb buffers in digital mode */
1026 alloc = 0;
1027 } else {
1028 usb_bufs = &dev->usb_ctl.analog_bufs;
1029 alloc = 1;
1032 if (alloc) {
1033 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1034 max_pkt_size, packet_multiplier);
1035 if (rc)
1036 return rc;
1039 if (xfer_bulk) {
1040 rc = usb_clear_halt(udev, usb_bufs->urb[0]->pipe);
1041 if (rc < 0) {
1042 dev_err(&dev->intf->dev,
1043 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1044 rc);
1045 em28xx_uninit_usb_xfer(dev, mode);
1046 return rc;
1050 init_waitqueue_head(&dma_q->wq);
1051 init_waitqueue_head(&vbi_dma_q->wq);
1053 em28xx_capture_start(dev, 1);
1055 /* submit urbs and enables IRQ */
1056 for (i = 0; i < usb_bufs->num_bufs; i++) {
1057 rc = usb_submit_urb(usb_bufs->urb[i], GFP_KERNEL);
1058 if (rc) {
1059 dev_err(&dev->intf->dev,
1060 "submit of urb %i failed (error=%i)\n", i, rc);
1061 em28xx_uninit_usb_xfer(dev, mode);
1062 return rc;
1066 return 0;
1068 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1071 * Device control list
1074 static LIST_HEAD(em28xx_devlist);
1075 static DEFINE_MUTEX(em28xx_devlist_mutex);
1078 * Extension interface
1081 static LIST_HEAD(em28xx_extension_devlist);
1083 int em28xx_register_extension(struct em28xx_ops *ops)
1085 struct em28xx *dev = NULL;
1087 mutex_lock(&em28xx_devlist_mutex);
1088 list_add_tail(&ops->next, &em28xx_extension_devlist);
1089 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1090 if (ops->init) {
1091 ops->init(dev);
1092 if (dev->dev_next)
1093 ops->init(dev->dev_next);
1096 mutex_unlock(&em28xx_devlist_mutex);
1097 pr_info("em28xx: Registered (%s) extension\n", ops->name);
1098 return 0;
1100 EXPORT_SYMBOL(em28xx_register_extension);
1102 void em28xx_unregister_extension(struct em28xx_ops *ops)
1104 struct em28xx *dev = NULL;
1106 mutex_lock(&em28xx_devlist_mutex);
1107 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1108 if (ops->fini) {
1109 if (dev->dev_next)
1110 ops->fini(dev->dev_next);
1111 ops->fini(dev);
1114 list_del(&ops->next);
1115 mutex_unlock(&em28xx_devlist_mutex);
1116 pr_info("em28xx: Removed (%s) extension\n", ops->name);
1118 EXPORT_SYMBOL(em28xx_unregister_extension);
1120 void em28xx_init_extension(struct em28xx *dev)
1122 const struct em28xx_ops *ops = NULL;
1124 mutex_lock(&em28xx_devlist_mutex);
1125 list_add_tail(&dev->devlist, &em28xx_devlist);
1126 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1127 if (ops->init) {
1128 ops->init(dev);
1129 if (dev->dev_next)
1130 ops->init(dev->dev_next);
1133 mutex_unlock(&em28xx_devlist_mutex);
1136 void em28xx_close_extension(struct em28xx *dev)
1138 const struct em28xx_ops *ops = NULL;
1140 mutex_lock(&em28xx_devlist_mutex);
1141 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1142 if (ops->fini) {
1143 if (dev->dev_next)
1144 ops->fini(dev->dev_next);
1145 ops->fini(dev);
1148 list_del(&dev->devlist);
1149 mutex_unlock(&em28xx_devlist_mutex);
1152 int em28xx_suspend_extension(struct em28xx *dev)
1154 const struct em28xx_ops *ops = NULL;
1156 dev_info(&dev->intf->dev, "Suspending extensions\n");
1157 mutex_lock(&em28xx_devlist_mutex);
1158 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1159 if (ops->suspend)
1160 ops->suspend(dev);
1161 if (dev->dev_next)
1162 ops->suspend(dev->dev_next);
1164 mutex_unlock(&em28xx_devlist_mutex);
1165 return 0;
1168 int em28xx_resume_extension(struct em28xx *dev)
1170 const struct em28xx_ops *ops = NULL;
1172 dev_info(&dev->intf->dev, "Resuming extensions\n");
1173 mutex_lock(&em28xx_devlist_mutex);
1174 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1175 if (!ops->resume)
1176 continue;
1177 ops->resume(dev);
1178 if (dev->dev_next)
1179 ops->resume(dev->dev_next);
1181 mutex_unlock(&em28xx_devlist_mutex);
1182 return 0;