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>
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.
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 { \
51 dev_printk(KERN_DEBUG, &dev->intf->dev, \
52 "core: %s: " fmt, __func__, ## arg); \
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 { \
61 dev_printk(KERN_DEBUG, &dev->intf->dev, \
62 "reg: %s: " fmt, __func__, ## arg); \
65 /* FIXME: don't abuse core_debug */
66 #define em28xx_isocdbg(fmt, arg...) do { \
68 dev_printk(KERN_DEBUG, &dev->intf->dev, \
69 "core: %s: " fmt, __func__, ## arg); \
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
,
80 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
81 int pipe
= usb_rcvctrlpipe(udev
, 0);
83 if (dev
->disconnected
)
86 if (len
> URB_MAX_CTRL_SIZE
)
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
);
94 em28xx_regdbg("(pipe 0x%08x): IN: %02x %02x %02x %02x %02x %02x %02x %02x failed with error %i\n",
96 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
99 len
& 0xff, len
>> 8, ret
);
100 mutex_unlock(&dev
->ctrl_urb_lock
);
101 return usb_translate_errors(ret
);
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
,
112 reg
& 0xff, reg
>> 8,
113 len
& 0xff, len
>> 8, len
, buf
);
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
)
127 ret
= em28xx_read_reg_req_len(dev
, req
, reg
, &val
, 1);
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
,
148 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
149 int pipe
= usb_sndctrlpipe(udev
, 0);
151 if (dev
->disconnected
)
154 if (len
< 1 || len
> URB_MAX_CTRL_SIZE
)
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
);
165 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph failed with error %i\n",
167 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
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",
176 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
178 reg
& 0xff, reg
>> 8,
179 len
& 0xff, len
>> 8, len
, buf
);
181 if (dev
->wait_after_write
)
182 msleep(dev
->wait_after_write
);
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
205 int em28xx_write_reg_bits(struct em28xx
*dev
, u16 reg
, u8 val
,
211 oldval
= em28xx_read_reg(dev
, reg
);
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
)
230 oldval
= em28xx_read_reg(dev
, reg
);
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
);
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
);
260 dev_warn(&dev
->intf
->dev
,
261 "AC97 command still being executed: not handled properly!\n");
267 * write a 16 bit value to the specified AC97 address (LSB first!)
269 int em28xx_read_ac97(struct em28xx
*dev
, u8 reg
)
272 u8 addr
= (reg
& 0x7f) | 0x80;
275 ret
= em28xx_is_ac97_ready(dev
);
279 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
283 ret
= dev
->em28xx_read_reg_req_len(dev
, 0, EM28XX_R40_AC97LSB
,
284 (u8
*)&val
, sizeof(val
));
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
)
299 u8 addr
= reg
& 0x7f;
302 value
= cpu_to_le16(val
);
304 ret
= em28xx_is_ac97_ready(dev
);
308 ret
= em28xx_write_regs(dev
, EM28XX_R40_AC97LSB
, (u8
*)&value
, 2);
312 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
318 EXPORT_SYMBOL_GPL(em28xx_write_ac97
);
320 struct em28xx_vol_itable
{
321 enum em28xx_amux mux
;
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
)
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);
352 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x8000);
355 dev_warn(&dev
->intf
->dev
,
356 "couldn't setup AC97 register %d\n",
362 static int em28xx_set_audio_source(struct em28xx
*dev
)
367 if (dev
->board
.is_em2800
) {
368 if (dev
->ctl_ainput
== EM28XX_AMUX_VIDEO
)
369 input
= EM2800_AUDIO_SRC_TUNER
;
371 input
= EM2800_AUDIO_SRC_LINE
;
373 ret
= em28xx_write_regs(dev
, EM2800_R08_AUDIOSRC
, &input
, 1);
378 if (dev
->has_msp34xx
) {
379 input
= EM28XX_AUDIO_SRC_TUNER
;
381 switch (dev
->ctl_ainput
) {
382 case EM28XX_AMUX_VIDEO
:
383 input
= EM28XX_AUDIO_SRC_TUNER
;
386 input
= EM28XX_AUDIO_SRC_LINE
;
391 if (dev
->board
.mute_gpio
&& dev
->mute
)
392 em28xx_gpio_set(dev
, dev
->board
.mute_gpio
);
394 em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
396 ret
= em28xx_write_reg_bits(dev
, EM28XX_R0E_AUDIOSRC
, input
, 0xc0);
399 usleep_range(10000, 11000);
401 switch (dev
->audio_mode
.ac97
) {
405 ret
= set_ac97_input(dev
);
411 struct em28xx_vol_otable
{
412 enum em28xx_aout mux
;
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
)
429 if (dev
->int_audio_type
== EM28XX_INT_AUDIO_NONE
)
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);
441 dev_warn(&dev
->intf
->dev
,
442 "couldn't setup AC97 register %d\n",
447 xclk
= dev
->board
.xclk
& 0x7f;
449 xclk
|= EM28XX_XCLK_AUDIO_UNMUTE
;
451 ret
= em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, xclk
);
454 usleep_range(10000, 11000);
456 /* Selects the proper audio input */
457 ret
= em28xx_set_audio_source(dev
);
460 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
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 */
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
,
480 dev_warn(&dev
->intf
->dev
,
481 "couldn't setup AC97 register %d\n",
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
494 em28xx_write_ac97(dev
, AC97_REC_SEL
, sel
);
500 EXPORT_SYMBOL_GPL(em28xx_audio_analog_set
);
502 int em28xx_audio_setup(struct em28xx
*dev
)
504 int vid1
, vid2
, feat
, cfg
;
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
;
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
;
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
)
535 else if (dev
->chip_id
>= CHIP_ID_EM2860
&&
536 (cfg
& EM28XX_CHIPCFG_AUDIOMASK
) ==
537 EM2860_CHIPCFG_I2S_5_SAMPRATES
)
541 dev_info(&dev
->intf
->dev
, "I2S Audio (%d sample rate(s))\n",
543 /* Skip the code that does AC97 vendor detection */
544 dev
->audio_mode
.ac97
= EM28XX_NO_AC97
;
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
);
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
;
568 vid2
= em28xx_read_ac97(dev
, AC97_VENDOR_ID2
);
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
);
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
;
588 /* Reports detected AC97 processor */
589 switch (dev
->audio_mode
.ac97
) {
591 dev_info(&dev
->intf
->dev
, "No AC97 audio processor\n");
593 case EM28XX_AC97_EM202
:
594 dev_info(&dev
->intf
->dev
,
595 "Empia 202 AC97 audio processor detected\n");
597 case EM28XX_AC97_SIGMATEL
:
598 dev_info(&dev
->intf
->dev
,
599 "Sigmatel audio processor detected (stac 97%02x)\n",
602 case EM28XX_AC97_OTHER
:
603 dev_warn(&dev
->intf
->dev
,
604 "Unknown AC97 audio processor detected!\n");
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
) {
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
];
629 EXPORT_SYMBOL_GPL(em28xx_find_led
);
631 int em28xx_capture_start(struct em28xx
*dev
, int start
)
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
,
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
);
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
);
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);
674 rc
= em28xx_write_reg(dev
, 0x13, 0x0c);
676 /* Enable video capture */
677 rc
= em28xx_write_reg(dev
, 0x48, 0x00);
681 if (dev
->mode
== EM28XX_ANALOG_MODE
)
682 rc
= em28xx_write_reg(dev
,
683 EM28XX_R12_VINENABLE
,
686 rc
= em28xx_write_reg(dev
,
687 EM28XX_R12_VINENABLE
,
692 usleep_range(10000, 11000);
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
);
702 led
= em28xx_find_led(dev
, EM28XX_LED_DIGITAL_CAPTURING
);
705 em28xx_write_reg_bits(dev
, led
->gpio_reg
,
706 (!start
^ led
->inverted
) ?
707 ~led
->gpio_mask
: led
->gpio_mask
,
713 int em28xx_gpio_set(struct em28xx
*dev
, const struct em28xx_reg_seq
*gpio
)
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);
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
,
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
)
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
);
766 return em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
768 EXPORT_SYMBOL_GPL(em28xx_set_mode
);
775 * URB completion handler for isoc/bulk transfers
777 static void em28xx_irq_callback(struct urb
*urb
)
779 struct em28xx
*dev
= urb
->context
;
783 switch (urb
->status
) {
784 case 0: /* success */
785 case -ETIMEDOUT
: /* NAK */
787 case -ECONNRESET
: /* kill */
792 em28xx_isocdbg("urb completion error %d.\n", urb
->status
);
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;
809 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
811 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
817 * Stop and Deallocate URBs
819 void em28xx_uninit_usb_xfer(struct em28xx
*dev
, enum em28xx_mode mode
)
822 struct em28xx_usb_bufs
*usb_bufs
;
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
;
830 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
832 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
833 urb
= usb_bufs
->urb
[i
];
835 if (!irqs_disabled())
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
);
859 void em28xx_stop_urbs(struct em28xx
*dev
)
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
];
870 if (!irqs_disabled())
877 em28xx_capture_start(dev
, 0);
879 EXPORT_SYMBOL_GPL(em28xx_stop_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
;
889 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
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",
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",
917 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
919 dev_err(&dev
->intf
->dev
, "invalid mode selected\n");
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
);
932 usb_bufs
->buf
= kcalloc(num_bufs
, sizeof(void *), GFP_KERNEL
);
933 if (!usb_bufs
->buf
) {
934 kfree(usb_bufs
->buf
);
938 usb_bufs
->max_pkt_size
= max_pkt_size
;
940 usb_bufs
->num_packets
= 0;
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
);
952 em28xx_uninit_usb_xfer(dev
, mode
);
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
;
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
:
977 usb_fill_bulk_urb(urb
, udev
, pipe
, usb_bufs
->buf
[i
],
978 sb_size
, em28xx_irq_callback
, dev
);
980 pipe
= usb_rcvisocpipe(udev
,
981 mode
== EM28XX_ANALOG_MODE
?
982 dev
->analog_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
;
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
;
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
);
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 */
1028 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
1033 rc
= em28xx_alloc_urbs(dev
, mode
, xfer_bulk
, num_bufs
,
1034 max_pkt_size
, packet_multiplier
);
1040 rc
= usb_clear_halt(udev
, usb_bufs
->urb
[0]->pipe
);
1042 dev_err(&dev
->intf
->dev
,
1043 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1045 em28xx_uninit_usb_xfer(dev
, mode
);
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
);
1059 dev_err(&dev
->intf
->dev
,
1060 "submit of urb %i failed (error=%i)\n", i
, rc
);
1061 em28xx_uninit_usb_xfer(dev
, mode
);
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
) {
1093 ops
->init(dev
->dev_next
);
1096 mutex_unlock(&em28xx_devlist_mutex
);
1097 pr_info("em28xx: Registered (%s) extension\n", ops
->name
);
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
) {
1110 ops
->fini(dev
->dev_next
);
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
) {
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
) {
1144 ops
->fini(dev
->dev_next
);
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
) {
1162 ops
->suspend(dev
->dev_next
);
1164 mutex_unlock(&em28xx_devlist_mutex
);
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
) {
1179 ops
->resume(dev
->dev_next
);
1181 mutex_unlock(&em28xx_devlist_mutex
);