1 // SPDX-License-Identifier: GPL-2.0+
3 // em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
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>
13 #include <linux/init.h>
14 #include <linux/jiffies.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
19 #include <linux/vmalloc.h>
20 #include <sound/ac97_codec.h>
21 #include <media/v4l2-common.h>
23 #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
24 "Markus Rechberger <mrechberger@gmail.com>, " \
25 "Mauro Carvalho Chehab <mchehab@kernel.org>, " \
26 "Sascha Sommer <saschasommer@freenet.de>"
28 MODULE_AUTHOR(DRIVER_AUTHOR
);
29 MODULE_DESCRIPTION(DRIVER_DESC
);
30 MODULE_LICENSE("GPL v2");
31 MODULE_VERSION(EM28XX_VERSION
);
33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
35 static unsigned int core_debug
;
36 module_param(core_debug
, int, 0644);
37 MODULE_PARM_DESC(core_debug
, "enable debug messages [core and isoc]");
39 #define em28xx_coredbg(fmt, arg...) do { \
41 dev_printk(KERN_DEBUG, &dev->intf->dev, \
42 "core: %s: " fmt, __func__, ## arg); \
45 static unsigned int reg_debug
;
46 module_param(reg_debug
, int, 0644);
47 MODULE_PARM_DESC(reg_debug
, "enable debug messages [URB reg]");
49 #define em28xx_regdbg(fmt, arg...) do { \
51 dev_printk(KERN_DEBUG, &dev->intf->dev, \
52 "reg: %s: " fmt, __func__, ## arg); \
55 /* FIXME: don't abuse core_debug */
56 #define em28xx_isocdbg(fmt, arg...) do { \
58 dev_printk(KERN_DEBUG, &dev->intf->dev, \
59 "core: %s: " fmt, __func__, ## arg); \
63 * em28xx_read_reg_req()
64 * reads data from the usb device specifying bRequest
66 int em28xx_read_reg_req_len(struct em28xx
*dev
, u8 req
, u16 reg
,
70 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
71 int pipe
= usb_rcvctrlpipe(udev
, 0);
73 if (dev
->disconnected
)
76 if (len
> URB_MAX_CTRL_SIZE
)
79 mutex_lock(&dev
->ctrl_urb_lock
);
80 ret
= usb_control_msg(udev
, pipe
, req
,
81 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
82 0x0000, reg
, dev
->urb_buf
, len
, 1000);
84 em28xx_regdbg("(pipe 0x%08x): IN: %02x %02x %02x %02x %02x %02x %02x %02x failed with error %i\n",
86 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
89 len
& 0xff, len
>> 8, ret
);
90 mutex_unlock(&dev
->ctrl_urb_lock
);
91 return usb_translate_errors(ret
);
95 memcpy(buf
, dev
->urb_buf
, len
);
97 mutex_unlock(&dev
->ctrl_urb_lock
);
99 em28xx_regdbg("(pipe 0x%08x): IN: %02x %02x %02x %02x %02x %02x %02x %02x <<< %*ph\n",
100 pipe
, USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
102 reg
& 0xff, reg
>> 8,
103 len
& 0xff, len
>> 8, len
, buf
);
109 * em28xx_read_reg_req()
110 * reads data from the usb device specifying bRequest
112 int em28xx_read_reg_req(struct em28xx
*dev
, u8 req
, u16 reg
)
117 ret
= em28xx_read_reg_req_len(dev
, req
, reg
, &val
, 1);
124 int em28xx_read_reg(struct em28xx
*dev
, u16 reg
)
126 return em28xx_read_reg_req(dev
, USB_REQ_GET_STATUS
, reg
);
128 EXPORT_SYMBOL_GPL(em28xx_read_reg
);
131 * em28xx_write_regs_req()
132 * sends data to the usb device, specifying bRequest
134 int em28xx_write_regs_req(struct em28xx
*dev
, u8 req
, u16 reg
, char *buf
,
138 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
139 int pipe
= usb_sndctrlpipe(udev
, 0);
141 if (dev
->disconnected
)
144 if (len
< 1 || len
> URB_MAX_CTRL_SIZE
)
147 mutex_lock(&dev
->ctrl_urb_lock
);
148 memcpy(dev
->urb_buf
, buf
, len
);
149 ret
= usb_control_msg(udev
, pipe
, req
,
150 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
151 0x0000, reg
, dev
->urb_buf
, len
, 1000);
152 mutex_unlock(&dev
->ctrl_urb_lock
);
155 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph failed with error %i\n",
157 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
159 reg
& 0xff, reg
>> 8,
160 len
& 0xff, len
>> 8, len
, buf
, ret
);
161 return usb_translate_errors(ret
);
164 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph\n",
166 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
168 reg
& 0xff, reg
>> 8,
169 len
& 0xff, len
>> 8, len
, buf
);
171 if (dev
->wait_after_write
)
172 msleep(dev
->wait_after_write
);
177 int em28xx_write_regs(struct em28xx
*dev
, u16 reg
, char *buf
, int len
)
179 return em28xx_write_regs_req(dev
, USB_REQ_GET_STATUS
, reg
, buf
, len
);
181 EXPORT_SYMBOL_GPL(em28xx_write_regs
);
183 /* Write a single register */
184 int em28xx_write_reg(struct em28xx
*dev
, u16 reg
, u8 val
)
186 return em28xx_write_regs(dev
, reg
, &val
, 1);
188 EXPORT_SYMBOL_GPL(em28xx_write_reg
);
191 * em28xx_write_reg_bits()
192 * sets only some bits (specified by bitmask) of a register, by first reading
195 int em28xx_write_reg_bits(struct em28xx
*dev
, u16 reg
, u8 val
,
201 oldval
= em28xx_read_reg(dev
, reg
);
205 newval
= (((u8
)oldval
) & ~bitmask
) | (val
& bitmask
);
207 return em28xx_write_regs(dev
, reg
, &newval
, 1);
209 EXPORT_SYMBOL_GPL(em28xx_write_reg_bits
);
212 * em28xx_toggle_reg_bits()
213 * toggles/inverts the bits (specified by bitmask) of a register
215 int em28xx_toggle_reg_bits(struct em28xx
*dev
, u16 reg
, u8 bitmask
)
220 oldval
= em28xx_read_reg(dev
, reg
);
224 newval
= (~oldval
& bitmask
) | (oldval
& ~bitmask
);
226 return em28xx_write_reg(dev
, reg
, newval
);
228 EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits
);
231 * em28xx_is_ac97_ready()
232 * Checks if ac97 is ready
234 static int em28xx_is_ac97_ready(struct em28xx
*dev
)
236 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT
);
239 /* Wait up to 50 ms for AC97 command to complete */
240 while (time_is_after_jiffies(timeout
)) {
241 ret
= em28xx_read_reg(dev
, EM28XX_R43_AC97BUSY
);
250 dev_warn(&dev
->intf
->dev
,
251 "AC97 command still being executed: not handled properly!\n");
257 * write a 16 bit value to the specified AC97 address (LSB first!)
259 int em28xx_read_ac97(struct em28xx
*dev
, u8 reg
)
262 u8 addr
= (reg
& 0x7f) | 0x80;
265 ret
= em28xx_is_ac97_ready(dev
);
269 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
273 ret
= dev
->em28xx_read_reg_req_len(dev
, 0, EM28XX_R40_AC97LSB
,
274 (u8
*)&val
, sizeof(val
));
278 return le16_to_cpu(val
);
280 EXPORT_SYMBOL_GPL(em28xx_read_ac97
);
283 * em28xx_write_ac97()
284 * write a 16 bit value to the specified AC97 address (LSB first!)
286 int em28xx_write_ac97(struct em28xx
*dev
, u8 reg
, u16 val
)
289 u8 addr
= reg
& 0x7f;
292 value
= cpu_to_le16(val
);
294 ret
= em28xx_is_ac97_ready(dev
);
298 ret
= em28xx_write_regs(dev
, EM28XX_R40_AC97LSB
, (u8
*)&value
, 2);
302 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
308 EXPORT_SYMBOL_GPL(em28xx_write_ac97
);
310 struct em28xx_vol_itable
{
311 enum em28xx_amux mux
;
315 static struct em28xx_vol_itable inputs
[] = {
316 { EM28XX_AMUX_VIDEO
, AC97_VIDEO
},
317 { EM28XX_AMUX_LINE_IN
, AC97_LINE
},
318 { EM28XX_AMUX_PHONE
, AC97_PHONE
},
319 { EM28XX_AMUX_MIC
, AC97_MIC
},
320 { EM28XX_AMUX_CD
, AC97_CD
},
321 { EM28XX_AMUX_AUX
, AC97_AUX
},
322 { EM28XX_AMUX_PCM_OUT
, AC97_PCM
},
325 static int set_ac97_input(struct em28xx
*dev
)
328 enum em28xx_amux amux
= dev
->ctl_ainput
;
331 * EM28XX_AMUX_VIDEO2 is a special case used to indicate that
332 * em28xx should point to LINE IN, while AC97 should use VIDEO
334 if (amux
== EM28XX_AMUX_VIDEO2
)
335 amux
= EM28XX_AMUX_VIDEO
;
337 /* Mute all entres but the one that were selected */
338 for (i
= 0; i
< ARRAY_SIZE(inputs
); i
++) {
339 if (amux
== inputs
[i
].mux
)
340 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x0808);
342 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x8000);
345 dev_warn(&dev
->intf
->dev
,
346 "couldn't setup AC97 register %d\n",
352 static int em28xx_set_audio_source(struct em28xx
*dev
)
357 if (dev
->board
.is_em2800
) {
358 if (dev
->ctl_ainput
== EM28XX_AMUX_VIDEO
)
359 input
= EM2800_AUDIO_SRC_TUNER
;
361 input
= EM2800_AUDIO_SRC_LINE
;
363 ret
= em28xx_write_regs(dev
, EM2800_R08_AUDIOSRC
, &input
, 1);
368 if (dev
->has_msp34xx
) {
369 input
= EM28XX_AUDIO_SRC_TUNER
;
371 switch (dev
->ctl_ainput
) {
372 case EM28XX_AMUX_VIDEO
:
373 input
= EM28XX_AUDIO_SRC_TUNER
;
376 input
= EM28XX_AUDIO_SRC_LINE
;
381 if (dev
->board
.mute_gpio
&& dev
->mute
)
382 em28xx_gpio_set(dev
, dev
->board
.mute_gpio
);
384 em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
386 ret
= em28xx_write_reg_bits(dev
, EM28XX_R0E_AUDIOSRC
, input
, 0xc0);
389 usleep_range(10000, 11000);
391 switch (dev
->audio_mode
.ac97
) {
395 ret
= set_ac97_input(dev
);
401 struct em28xx_vol_otable
{
402 enum em28xx_aout mux
;
406 static const struct em28xx_vol_otable outputs
[] = {
407 { EM28XX_AOUT_MASTER
, AC97_MASTER
},
408 { EM28XX_AOUT_LINE
, AC97_HEADPHONE
},
409 { EM28XX_AOUT_MONO
, AC97_MASTER_MONO
},
410 { EM28XX_AOUT_LFE
, AC97_CENTER_LFE_MASTER
},
411 { EM28XX_AOUT_SURR
, AC97_SURROUND_MASTER
},
414 int em28xx_audio_analog_set(struct em28xx
*dev
)
419 /* Set GPIOs here for boards without audio */
420 if (dev
->int_audio_type
== EM28XX_INT_AUDIO_NONE
)
421 return em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
424 * It is assumed that all devices use master volume for output.
425 * It would be possible to use also line output.
427 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
428 /* Mute all outputs */
429 for (i
= 0; i
< ARRAY_SIZE(outputs
); i
++) {
430 ret
= em28xx_write_ac97(dev
, outputs
[i
].reg
, 0x8000);
432 dev_warn(&dev
->intf
->dev
,
433 "couldn't setup AC97 register %d\n",
438 xclk
= dev
->board
.xclk
& 0x7f;
440 xclk
|= EM28XX_XCLK_AUDIO_UNMUTE
;
442 ret
= em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, xclk
);
445 usleep_range(10000, 11000);
447 /* Selects the proper audio input */
448 ret
= em28xx_set_audio_source(dev
);
451 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
454 em28xx_write_ac97(dev
, AC97_POWERDOWN
, 0x4200);
455 em28xx_write_ac97(dev
, AC97_EXTENDED_STATUS
, 0x0031);
456 em28xx_write_ac97(dev
, AC97_PCM_LR_ADC_RATE
, 0xbb80);
458 /* LSB: left channel - both channels with the same level */
459 vol
= (0x1f - dev
->volume
) | ((0x1f - dev
->volume
) << 8);
461 /* Mute device, if needed */
466 for (i
= 0; i
< ARRAY_SIZE(outputs
); i
++) {
467 if (dev
->ctl_aoutput
& outputs
[i
].mux
)
468 ret
= em28xx_write_ac97(dev
, outputs
[i
].reg
,
471 dev_warn(&dev
->intf
->dev
,
472 "couldn't setup AC97 register %d\n",
476 if (dev
->ctl_aoutput
& EM28XX_AOUT_PCM_IN
) {
477 int sel
= ac97_return_record_select(dev
->ctl_aoutput
);
480 * Use the same input for both left and right
485 em28xx_write_ac97(dev
, AC97_REC_SEL
, sel
);
491 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set
);
493 int em28xx_audio_setup(struct em28xx
*dev
)
495 int vid1
, vid2
, feat
, cfg
;
499 if (dev
->chip_id
== CHIP_ID_EM2870
||
500 dev
->chip_id
== CHIP_ID_EM2874
||
501 dev
->chip_id
== CHIP_ID_EM28174
||
502 dev
->chip_id
== CHIP_ID_EM28178
) {
503 /* Digital only device - don't load any alsa module */
504 dev
->int_audio_type
= EM28XX_INT_AUDIO_NONE
;
505 dev
->usb_audio_type
= EM28XX_USB_AUDIO_NONE
;
509 /* See how this device is configured */
510 cfg
= em28xx_read_reg(dev
, EM28XX_R00_CHIPCFG
);
511 dev_info(&dev
->intf
->dev
, "Config register raw data: 0x%02x\n", cfg
);
512 if (cfg
< 0) { /* Register read error */
513 /* Be conservative */
514 dev
->int_audio_type
= EM28XX_INT_AUDIO_AC97
;
515 } else if ((cfg
& EM28XX_CHIPCFG_AUDIOMASK
) == 0x00) {
516 /* The device doesn't have vendor audio at all */
517 dev
->int_audio_type
= EM28XX_INT_AUDIO_NONE
;
518 dev
->usb_audio_type
= EM28XX_USB_AUDIO_NONE
;
520 } else if ((cfg
& EM28XX_CHIPCFG_AUDIOMASK
) != EM28XX_CHIPCFG_AC97
) {
521 dev
->int_audio_type
= EM28XX_INT_AUDIO_I2S
;
522 if (dev
->chip_id
< CHIP_ID_EM2860
&&
523 (cfg
& EM28XX_CHIPCFG_AUDIOMASK
) ==
524 EM2820_CHIPCFG_I2S_1_SAMPRATE
)
526 else if (dev
->chip_id
>= CHIP_ID_EM2860
&&
527 (cfg
& EM28XX_CHIPCFG_AUDIOMASK
) ==
528 EM2860_CHIPCFG_I2S_5_SAMPRATES
)
532 dev_info(&dev
->intf
->dev
, "I2S Audio (%d sample rate(s))\n",
534 /* Skip the code that does AC97 vendor detection */
535 dev
->audio_mode
.ac97
= EM28XX_NO_AC97
;
538 dev
->int_audio_type
= EM28XX_INT_AUDIO_AC97
;
541 dev
->audio_mode
.ac97
= EM28XX_AC97_OTHER
;
543 vid1
= em28xx_read_ac97(dev
, AC97_VENDOR_ID1
);
546 * Device likely doesn't support AC97
547 * Note: (some) em2800 devices without eeprom reports 0x91 on
548 * CHIPCFG register, even not having an AC97 chip
550 dev_warn(&dev
->intf
->dev
,
551 "AC97 chip type couldn't be determined\n");
552 dev
->audio_mode
.ac97
= EM28XX_NO_AC97
;
553 if (dev
->usb_audio_type
== EM28XX_USB_AUDIO_VENDOR
)
554 dev
->usb_audio_type
= EM28XX_USB_AUDIO_NONE
;
555 dev
->int_audio_type
= EM28XX_INT_AUDIO_NONE
;
559 vid2
= em28xx_read_ac97(dev
, AC97_VENDOR_ID2
);
563 vid
= vid1
<< 16 | vid2
;
564 dev_warn(&dev
->intf
->dev
, "AC97 vendor ID = 0x%08x\n", vid
);
566 feat
= em28xx_read_ac97(dev
, AC97_RESET
);
570 dev_warn(&dev
->intf
->dev
, "AC97 features = 0x%04x\n", feat
);
572 /* Try to identify what audio processor we have */
573 if ((vid
== 0xffffffff || vid
== 0x83847650) && feat
== 0x6a90)
574 dev
->audio_mode
.ac97
= EM28XX_AC97_EM202
;
575 else if ((vid
>> 8) == 0x838476)
576 dev
->audio_mode
.ac97
= EM28XX_AC97_SIGMATEL
;
579 /* Reports detected AC97 processor */
580 switch (dev
->audio_mode
.ac97
) {
582 dev_info(&dev
->intf
->dev
, "No AC97 audio processor\n");
584 case EM28XX_AC97_EM202
:
585 dev_info(&dev
->intf
->dev
,
586 "Empia 202 AC97 audio processor detected\n");
588 case EM28XX_AC97_SIGMATEL
:
589 dev_info(&dev
->intf
->dev
,
590 "Sigmatel audio processor detected (stac 97%02x)\n",
593 case EM28XX_AC97_OTHER
:
594 dev_warn(&dev
->intf
->dev
,
595 "Unknown AC97 audio processor detected!\n");
601 return em28xx_audio_analog_set(dev
);
603 EXPORT_SYMBOL_GPL(em28xx_audio_setup
);
605 const struct em28xx_led
*em28xx_find_led(struct em28xx
*dev
,
606 enum em28xx_led_role role
)
608 if (dev
->board
.leds
) {
611 while (dev
->board
.leds
[k
].role
>= 0 &&
612 dev
->board
.leds
[k
].role
< EM28XX_NUM_LED_ROLES
) {
613 if (dev
->board
.leds
[k
].role
== role
)
614 return &dev
->board
.leds
[k
];
620 EXPORT_SYMBOL_GPL(em28xx_find_led
);
622 int em28xx_capture_start(struct em28xx
*dev
, int start
)
625 const struct em28xx_led
*led
= NULL
;
627 if (dev
->chip_id
== CHIP_ID_EM2874
||
628 dev
->chip_id
== CHIP_ID_EM2884
||
629 dev
->chip_id
== CHIP_ID_EM28174
||
630 dev
->chip_id
== CHIP_ID_EM28178
) {
631 /* The Transport Stream Enable Register moved in em2874 */
632 if (dev
->dvb_xfer_bulk
) {
633 /* Max Tx Size = 188 * 256 = 48128 - LCM(188,512) * 2 */
634 em28xx_write_reg(dev
, (dev
->ts
== PRIMARY_TS
) ?
635 EM2874_R5D_TS1_PKT_SIZE
:
636 EM2874_R5E_TS2_PKT_SIZE
,
639 /* ISOC Maximum Transfer Size = 188 * 5 */
640 em28xx_write_reg(dev
, (dev
->ts
== PRIMARY_TS
) ?
641 EM2874_R5D_TS1_PKT_SIZE
:
642 EM2874_R5E_TS2_PKT_SIZE
,
643 dev
->dvb_max_pkt_size_isoc
/ 188);
645 if (dev
->ts
== PRIMARY_TS
)
646 rc
= em28xx_write_reg_bits(dev
,
647 EM2874_R5F_TS_ENABLE
,
648 start
? EM2874_TS1_CAPTURE_ENABLE
: 0x00,
649 EM2874_TS1_CAPTURE_ENABLE
| EM2874_TS1_FILTER_ENABLE
| EM2874_TS1_NULL_DISCARD
);
651 rc
= em28xx_write_reg_bits(dev
,
652 EM2874_R5F_TS_ENABLE
,
653 start
? EM2874_TS2_CAPTURE_ENABLE
: 0x00,
654 EM2874_TS2_CAPTURE_ENABLE
| EM2874_TS2_FILTER_ENABLE
| EM2874_TS2_NULL_DISCARD
);
656 /* FIXME: which is the best order? */
657 /* video registers are sampled by VREF */
658 rc
= em28xx_write_reg_bits(dev
, EM28XX_R0C_USBSUSP
,
659 start
? 0x10 : 0x00, 0x10);
665 rc
= em28xx_write_reg(dev
, 0x13, 0x0c);
667 /* Enable video capture */
668 rc
= em28xx_write_reg(dev
, 0x48, 0x00);
672 if (dev
->mode
== EM28XX_ANALOG_MODE
)
673 rc
= em28xx_write_reg(dev
,
674 EM28XX_R12_VINENABLE
,
677 rc
= em28xx_write_reg(dev
,
678 EM28XX_R12_VINENABLE
,
683 usleep_range(10000, 11000);
685 /* disable video capture */
686 rc
= em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x27);
690 if (dev
->mode
== EM28XX_ANALOG_MODE
)
691 led
= em28xx_find_led(dev
, EM28XX_LED_ANALOG_CAPTURING
);
692 else if (dev
->ts
== PRIMARY_TS
)
693 led
= em28xx_find_led(dev
, EM28XX_LED_DIGITAL_CAPTURING
);
695 led
= em28xx_find_led(dev
, EM28XX_LED_DIGITAL_CAPTURING_TS2
);
698 em28xx_write_reg_bits(dev
, led
->gpio_reg
,
699 (!start
^ led
->inverted
) ?
700 ~led
->gpio_mask
: led
->gpio_mask
,
706 int em28xx_gpio_set(struct em28xx
*dev
, const struct em28xx_reg_seq
*gpio
)
713 if (dev
->mode
!= EM28XX_SUSPEND
) {
714 em28xx_write_reg(dev
, 0x48, 0x00);
715 if (dev
->mode
== EM28XX_ANALOG_MODE
)
716 em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x67);
718 em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x37);
719 usleep_range(10000, 11000);
722 /* Send GPIO reset sequences specified at board entry */
723 while (gpio
->sleep
>= 0) {
724 if (gpio
->reg
>= 0) {
725 rc
= em28xx_write_reg_bits(dev
,
739 EXPORT_SYMBOL_GPL(em28xx_gpio_set
);
741 int em28xx_set_mode(struct em28xx
*dev
, enum em28xx_mode set_mode
)
743 if (dev
->mode
== set_mode
)
746 if (set_mode
== EM28XX_SUSPEND
) {
747 dev
->mode
= set_mode
;
749 /* FIXME: add suspend support for ac97 */
751 return em28xx_gpio_set(dev
, dev
->board
.suspend_gpio
);
754 dev
->mode
= set_mode
;
756 if (dev
->mode
== EM28XX_DIGITAL_MODE
)
757 return em28xx_gpio_set(dev
, dev
->board
.dvb_gpio
);
759 return em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
761 EXPORT_SYMBOL_GPL(em28xx_set_mode
);
768 * URB completion handler for isoc/bulk transfers
770 static void em28xx_irq_callback(struct urb
*urb
)
772 struct em28xx
*dev
= urb
->context
;
776 switch (urb
->status
) {
777 case 0: /* success */
778 case -ETIMEDOUT
: /* NAK */
780 case -ECONNRESET
: /* kill */
785 em28xx_isocdbg("urb completion error %d.\n", urb
->status
);
789 /* Copy data from URB */
790 spin_lock_irqsave(&dev
->slock
, flags
);
791 dev
->usb_ctl
.urb_data_copy(dev
, urb
);
792 spin_unlock_irqrestore(&dev
->slock
, flags
);
794 /* Reset urb buffers */
795 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
796 /* isoc only (bulk: number_of_packets = 0) */
797 urb
->iso_frame_desc
[i
].status
= 0;
798 urb
->iso_frame_desc
[i
].actual_length
= 0;
802 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
804 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
810 * Stop and Deallocate URBs
812 void em28xx_uninit_usb_xfer(struct em28xx
*dev
, enum em28xx_mode mode
)
815 struct em28xx_usb_bufs
*usb_bufs
;
818 em28xx_isocdbg("called %s in mode %d\n", __func__
, mode
);
820 if (mode
== EM28XX_DIGITAL_MODE
)
821 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
823 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
825 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
826 urb
= usb_bufs
->urb
[i
];
828 if (!irqs_disabled())
834 usb_bufs
->urb
[i
] = NULL
;
838 kfree(usb_bufs
->urb
);
839 kfree(usb_bufs
->buf
);
841 usb_bufs
->urb
= NULL
;
842 usb_bufs
->buf
= NULL
;
843 usb_bufs
->num_bufs
= 0;
845 em28xx_capture_start(dev
, 0);
847 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer
);
852 void em28xx_stop_urbs(struct em28xx
*dev
)
856 struct em28xx_usb_bufs
*isoc_bufs
= &dev
->usb_ctl
.digital_bufs
;
858 em28xx_isocdbg("called %s\n", __func__
);
860 for (i
= 0; i
< isoc_bufs
->num_bufs
; i
++) {
861 urb
= isoc_bufs
->urb
[i
];
863 if (!irqs_disabled())
870 em28xx_capture_start(dev
, 0);
872 EXPORT_SYMBOL_GPL(em28xx_stop_urbs
);
877 int em28xx_alloc_urbs(struct em28xx
*dev
, enum em28xx_mode mode
, int xfer_bulk
,
878 int num_bufs
, int max_pkt_size
, int packet_multiplier
)
880 struct em28xx_usb_bufs
*usb_bufs
;
882 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
887 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__
, mode
);
890 * Check mode and if we have an endpoint for the selected
891 * transfer type, select buffer
893 if (mode
== EM28XX_DIGITAL_MODE
) {
894 if ((xfer_bulk
&& !dev
->dvb_ep_bulk
) ||
895 (!xfer_bulk
&& !dev
->dvb_ep_isoc
)) {
896 dev_err(&dev
->intf
->dev
,
897 "no endpoint for DVB mode and transfer type %d\n",
901 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
902 } else if (mode
== EM28XX_ANALOG_MODE
) {
903 if ((xfer_bulk
&& !dev
->analog_ep_bulk
) ||
904 (!xfer_bulk
&& !dev
->analog_ep_isoc
)) {
905 dev_err(&dev
->intf
->dev
,
906 "no endpoint for analog mode and transfer type %d\n",
910 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
912 dev_err(&dev
->intf
->dev
, "invalid mode selected\n");
916 /* De-allocates all pending stuff */
917 em28xx_uninit_usb_xfer(dev
, mode
);
919 usb_bufs
->num_bufs
= num_bufs
;
921 usb_bufs
->urb
= kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
925 usb_bufs
->buf
= kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
926 if (!usb_bufs
->buf
) {
927 kfree(usb_bufs
->urb
);
931 usb_bufs
->max_pkt_size
= max_pkt_size
;
933 usb_bufs
->num_packets
= 0;
935 usb_bufs
->num_packets
= packet_multiplier
;
936 dev
->usb_ctl
.vid_buf
= NULL
;
937 dev
->usb_ctl
.vbi_buf
= NULL
;
939 sb_size
= packet_multiplier
* usb_bufs
->max_pkt_size
;
941 /* allocate urbs and transfer buffers */
942 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
943 urb
= usb_alloc_urb(usb_bufs
->num_packets
, GFP_KERNEL
);
945 em28xx_uninit_usb_xfer(dev
, mode
);
948 usb_bufs
->urb
[i
] = urb
;
950 usb_bufs
->buf
[i
] = kzalloc(sb_size
, GFP_KERNEL
);
951 if (!usb_bufs
->buf
[i
]) {
952 for (i
--; i
>= 0; i
--)
953 kfree(usb_bufs
->buf
[i
]);
955 em28xx_uninit_usb_xfer(dev
, mode
);
959 urb
->transfer_flags
= URB_FREE_BUFFER
;
961 if (xfer_bulk
) { /* bulk */
962 pipe
= usb_rcvbulkpipe(udev
,
963 mode
== EM28XX_ANALOG_MODE
?
964 dev
->analog_ep_bulk
:
966 usb_fill_bulk_urb(urb
, udev
, pipe
, usb_bufs
->buf
[i
],
967 sb_size
, em28xx_irq_callback
, dev
);
969 pipe
= usb_rcvisocpipe(udev
,
970 mode
== EM28XX_ANALOG_MODE
?
971 dev
->analog_ep_isoc
:
973 usb_fill_int_urb(urb
, udev
, pipe
, usb_bufs
->buf
[i
],
974 sb_size
, em28xx_irq_callback
, dev
, 1);
975 urb
->transfer_flags
|= URB_ISO_ASAP
;
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
;
985 urb
->number_of_packets
= usb_bufs
->num_packets
;
990 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs
);
993 * Allocate URBs and start IRQ
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
))
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
);
1008 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__
, mode
);
1010 dev
->usb_ctl
.urb_data_copy
= urb_data_copy
;
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 */
1017 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
1022 rc
= em28xx_alloc_urbs(dev
, mode
, xfer_bulk
, num_bufs
,
1023 max_pkt_size
, packet_multiplier
);
1029 rc
= usb_clear_halt(udev
, usb_bufs
->urb
[0]->pipe
);
1031 dev_err(&dev
->intf
->dev
,
1032 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1034 em28xx_uninit_usb_xfer(dev
, mode
);
1039 init_waitqueue_head(&dma_q
->wq
);
1040 init_waitqueue_head(&vbi_dma_q
->wq
);
1042 em28xx_capture_start(dev
, 1);
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_KERNEL
);
1048 dev_err(&dev
->intf
->dev
,
1049 "submit of urb %i failed (error=%i)\n", i
, rc
);
1050 em28xx_uninit_usb_xfer(dev
, mode
);
1057 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer
);
1060 * Device control list
1063 static LIST_HEAD(em28xx_devlist
);
1064 static DEFINE_MUTEX(em28xx_devlist_mutex
);
1067 * Extension interface
1070 static LIST_HEAD(em28xx_extension_devlist
);
1072 int em28xx_register_extension(struct em28xx_ops
*ops
)
1074 struct em28xx
*dev
= NULL
;
1076 mutex_lock(&em28xx_devlist_mutex
);
1077 list_add_tail(&ops
->next
, &em28xx_extension_devlist
);
1078 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1082 ops
->init(dev
->dev_next
);
1085 mutex_unlock(&em28xx_devlist_mutex
);
1086 pr_info("em28xx: Registered (%s) extension\n", ops
->name
);
1089 EXPORT_SYMBOL(em28xx_register_extension
);
1091 void em28xx_unregister_extension(struct em28xx_ops
*ops
)
1093 struct em28xx
*dev
= NULL
;
1095 mutex_lock(&em28xx_devlist_mutex
);
1096 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1099 ops
->fini(dev
->dev_next
);
1103 list_del(&ops
->next
);
1104 mutex_unlock(&em28xx_devlist_mutex
);
1105 pr_info("em28xx: Removed (%s) extension\n", ops
->name
);
1107 EXPORT_SYMBOL(em28xx_unregister_extension
);
1109 void em28xx_init_extension(struct em28xx
*dev
)
1111 const struct em28xx_ops
*ops
= NULL
;
1113 mutex_lock(&em28xx_devlist_mutex
);
1114 list_add_tail(&dev
->devlist
, &em28xx_devlist
);
1115 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1119 ops
->init(dev
->dev_next
);
1122 mutex_unlock(&em28xx_devlist_mutex
);
1125 void em28xx_close_extension(struct em28xx
*dev
)
1127 const struct em28xx_ops
*ops
= NULL
;
1129 mutex_lock(&em28xx_devlist_mutex
);
1130 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1133 ops
->fini(dev
->dev_next
);
1137 list_del(&dev
->devlist
);
1138 mutex_unlock(&em28xx_devlist_mutex
);
1141 int em28xx_suspend_extension(struct em28xx
*dev
)
1143 const struct em28xx_ops
*ops
= NULL
;
1145 dev_info(&dev
->intf
->dev
, "Suspending extensions\n");
1146 mutex_lock(&em28xx_devlist_mutex
);
1147 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1152 ops
->suspend(dev
->dev_next
);
1154 mutex_unlock(&em28xx_devlist_mutex
);
1158 int em28xx_resume_extension(struct em28xx
*dev
)
1160 const struct em28xx_ops
*ops
= NULL
;
1162 dev_info(&dev
->intf
->dev
, "Resuming extensions\n");
1163 mutex_lock(&em28xx_devlist_mutex
);
1164 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1169 ops
->resume(dev
->dev_next
);
1171 mutex_unlock(&em28xx_devlist_mutex
);