2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
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>
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.
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.
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.
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>
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>"
42 MODULE_AUTHOR(DRIVER_AUTHOR
);
43 MODULE_DESCRIPTION(DRIVER_DESC
);
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(EM28XX_VERSION
);
47 /* #define ENABLE_DEBUG_ISOC_FRAMES */
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]");
53 #define em28xx_coredbg(fmt, arg...) do { \
55 dev_printk(KERN_DEBUG, &dev->intf->dev, \
56 "core: %s: " fmt, __func__, ## arg); \
59 static unsigned int reg_debug
;
60 module_param(reg_debug
, int, 0644);
61 MODULE_PARM_DESC(reg_debug
, "enable debug messages [URB reg]");
64 #define em28xx_regdbg(fmt, arg...) do { \
66 dev_printk(KERN_DEBUG, &dev->intf->dev, \
67 "reg: %s: " fmt, __func__, ## arg); \
70 /* FIXME: don't abuse core_debug */
71 #define em28xx_isocdbg(fmt, arg...) do { \
73 dev_printk(KERN_DEBUG, &dev->intf->dev, \
74 "core: %s: " fmt, __func__, ## arg); \
78 * em28xx_read_reg_req()
79 * reads data from the usb device specifying bRequest
81 int em28xx_read_reg_req_len(struct em28xx
*dev
, u8 req
, u16 reg
,
85 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
86 int pipe
= usb_rcvctrlpipe(udev
, 0);
88 if (dev
->disconnected
)
91 if (len
> URB_MAX_CTRL_SIZE
)
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
);
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
,
102 reg
& 0xff, reg
>> 8,
103 len
& 0xff, len
>> 8, ret
);
104 mutex_unlock(&dev
->ctrl_urb_lock
);
105 return usb_translate_errors(ret
);
109 memcpy(buf
, dev
->urb_buf
, len
);
111 mutex_unlock(&dev
->ctrl_urb_lock
);
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
,
116 reg
& 0xff, reg
>> 8,
117 len
& 0xff, len
>> 8, len
, buf
);
123 * em28xx_read_reg_req()
124 * reads data from the usb device specifying bRequest
126 int em28xx_read_reg_req(struct em28xx
*dev
, u8 req
, u16 reg
)
131 ret
= em28xx_read_reg_req_len(dev
, req
, reg
, &val
, 1);
138 int em28xx_read_reg(struct em28xx
*dev
, u16 reg
)
140 return em28xx_read_reg_req(dev
, USB_REQ_GET_STATUS
, reg
);
142 EXPORT_SYMBOL_GPL(em28xx_read_reg
);
145 * em28xx_write_regs_req()
146 * sends data to the usb device, specifying bRequest
148 int em28xx_write_regs_req(struct em28xx
*dev
, u8 req
, u16 reg
, char *buf
,
152 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
153 int pipe
= usb_sndctrlpipe(udev
, 0);
155 if (dev
->disconnected
)
158 if ((len
< 1) || (len
> URB_MAX_CTRL_SIZE
))
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
);
169 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph failed with error %i\n",
171 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
173 reg
& 0xff, reg
>> 8,
174 len
& 0xff, len
>> 8, len
, buf
, ret
);
175 return usb_translate_errors(ret
);
178 em28xx_regdbg("(pipe 0x%08x): OUT: %02x %02x %02x %02x %02x %02x %02x %02x >>> %*ph\n",
180 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
182 reg
& 0xff, reg
>> 8,
183 len
& 0xff, len
>> 8, len
, buf
);
185 if (dev
->wait_after_write
)
186 msleep(dev
->wait_after_write
);
191 int em28xx_write_regs(struct em28xx
*dev
, u16 reg
, char *buf
, int len
)
193 return em28xx_write_regs_req(dev
, USB_REQ_GET_STATUS
, reg
, buf
, len
);
195 EXPORT_SYMBOL_GPL(em28xx_write_regs
);
197 /* Write a single register */
198 int em28xx_write_reg(struct em28xx
*dev
, u16 reg
, u8 val
)
200 return em28xx_write_regs(dev
, reg
, &val
, 1);
202 EXPORT_SYMBOL_GPL(em28xx_write_reg
);
205 * em28xx_write_reg_bits()
206 * sets only some bits (specified by bitmask) of a register, by first reading
209 int em28xx_write_reg_bits(struct em28xx
*dev
, u16 reg
, u8 val
,
215 oldval
= em28xx_read_reg(dev
, reg
);
219 newval
= (((u8
)oldval
) & ~bitmask
) | (val
& bitmask
);
221 return em28xx_write_regs(dev
, reg
, &newval
, 1);
223 EXPORT_SYMBOL_GPL(em28xx_write_reg_bits
);
226 * em28xx_toggle_reg_bits()
227 * toggles/inverts the bits (specified by bitmask) of a register
229 int em28xx_toggle_reg_bits(struct em28xx
*dev
, u16 reg
, u8 bitmask
)
234 oldval
= em28xx_read_reg(dev
, reg
);
238 newval
= (~oldval
& bitmask
) | (oldval
& ~bitmask
);
240 return em28xx_write_reg(dev
, reg
, newval
);
242 EXPORT_SYMBOL_GPL(em28xx_toggle_reg_bits
);
245 * em28xx_is_ac97_ready()
246 * Checks if ac97 is ready
248 static int em28xx_is_ac97_ready(struct em28xx
*dev
)
250 unsigned long timeout
= jiffies
+ msecs_to_jiffies(EM28XX_AC97_XFER_TIMEOUT
);
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
);
264 dev_warn(&dev
->intf
->dev
,
265 "AC97 command still being executed: not handled properly!\n");
271 * write a 16 bit value to the specified AC97 address (LSB first!)
273 int em28xx_read_ac97(struct em28xx
*dev
, u8 reg
)
276 u8 addr
= (reg
& 0x7f) | 0x80;
279 ret
= em28xx_is_ac97_ready(dev
);
283 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
287 ret
= dev
->em28xx_read_reg_req_len(dev
, 0, EM28XX_R40_AC97LSB
,
288 (u8
*)&val
, sizeof(val
));
292 return le16_to_cpu(val
);
294 EXPORT_SYMBOL_GPL(em28xx_read_ac97
);
297 * em28xx_write_ac97()
298 * write a 16 bit value to the specified AC97 address (LSB first!)
300 int em28xx_write_ac97(struct em28xx
*dev
, u8 reg
, u16 val
)
303 u8 addr
= reg
& 0x7f;
306 value
= cpu_to_le16(val
);
308 ret
= em28xx_is_ac97_ready(dev
);
312 ret
= em28xx_write_regs(dev
, EM28XX_R40_AC97LSB
, (u8
*)&value
, 2);
316 ret
= em28xx_write_regs(dev
, EM28XX_R42_AC97ADDR
, &addr
, 1);
322 EXPORT_SYMBOL_GPL(em28xx_write_ac97
);
324 struct em28xx_vol_itable
{
325 enum em28xx_amux mux
;
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
},
339 static int set_ac97_input(struct em28xx
*dev
)
342 enum em28xx_amux amux
= dev
->ctl_ainput
;
344 /* EM28XX_AMUX_VIDEO2 is a special case used to indicate that
345 em28xx should point to LINE IN, while AC97 should use VIDEO
347 if (amux
== EM28XX_AMUX_VIDEO2
)
348 amux
= EM28XX_AMUX_VIDEO
;
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);
355 ret
= em28xx_write_ac97(dev
, inputs
[i
].reg
, 0x8000);
358 dev_warn(&dev
->intf
->dev
,
359 "couldn't setup AC97 register %d\n",
365 static int em28xx_set_audio_source(struct em28xx
*dev
)
370 if (dev
->board
.is_em2800
) {
371 if (dev
->ctl_ainput
== EM28XX_AMUX_VIDEO
)
372 input
= EM2800_AUDIO_SRC_TUNER
;
374 input
= EM2800_AUDIO_SRC_LINE
;
376 ret
= em28xx_write_regs(dev
, EM2800_R08_AUDIOSRC
, &input
, 1);
381 if (dev
->board
.has_msp34xx
)
382 input
= EM28XX_AUDIO_SRC_TUNER
;
384 switch (dev
->ctl_ainput
) {
385 case EM28XX_AMUX_VIDEO
:
386 input
= EM28XX_AUDIO_SRC_TUNER
;
389 input
= EM28XX_AUDIO_SRC_LINE
;
394 if (dev
->board
.mute_gpio
&& dev
->mute
)
395 em28xx_gpio_set(dev
, dev
->board
.mute_gpio
);
397 em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
399 ret
= em28xx_write_reg_bits(dev
, EM28XX_R0E_AUDIOSRC
, input
, 0xc0);
404 switch (dev
->audio_mode
.ac97
) {
408 ret
= set_ac97_input(dev
);
414 struct em28xx_vol_otable
{
415 enum em28xx_aout mux
;
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
},
427 int em28xx_audio_analog_set(struct em28xx
*dev
)
432 if (dev
->int_audio_type
== EM28XX_INT_AUDIO_NONE
)
435 /* It is assumed that all devices use master volume for output.
436 It would be possible to use also line output.
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);
443 dev_warn(&dev
->intf
->dev
,
444 "couldn't setup AC97 register %d\n",
449 xclk
= dev
->board
.xclk
& 0x7f;
451 xclk
|= EM28XX_XCLK_AUDIO_UNMUTE
;
453 ret
= em28xx_write_reg(dev
, EM28XX_R0F_XCLK
, xclk
);
458 /* Selects the proper audio input */
459 ret
= em28xx_set_audio_source(dev
);
462 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
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);
469 /* LSB: left channel - both channels with the same level */
470 vol
= (0x1f - dev
->volume
) | ((0x1f - dev
->volume
) << 8);
472 /* Mute device, if needed */
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
,
482 dev_warn(&dev
->intf
->dev
,
483 "couldn't setup AC97 register %d\n",
487 if (dev
->ctl_aoutput
& EM28XX_AOUT_PCM_IN
) {
488 int sel
= ac97_return_record_select(dev
->ctl_aoutput
);
490 /* 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 rc
= em28xx_write_reg_bits(dev
, EM2874_R5F_TS_ENABLE
,
643 EM2874_TS1_CAPTURE_ENABLE
: 0x00,
644 EM2874_TS1_CAPTURE_ENABLE
);
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);
654 if (dev
->board
.is_webcam
)
655 rc
= em28xx_write_reg(dev
, 0x13, 0x0c);
657 /* Enable video capture */
658 rc
= em28xx_write_reg(dev
, 0x48, 0x00);
662 if (dev
->mode
== EM28XX_ANALOG_MODE
)
663 rc
= em28xx_write_reg(dev
,
664 EM28XX_R12_VINENABLE
,
667 rc
= em28xx_write_reg(dev
,
668 EM28XX_R12_VINENABLE
,
675 /* disable video capture */
676 rc
= em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x27);
680 if (dev
->mode
== EM28XX_ANALOG_MODE
)
681 led
= em28xx_find_led(dev
, EM28XX_LED_ANALOG_CAPTURING
);
683 led
= em28xx_find_led(dev
, EM28XX_LED_DIGITAL_CAPTURING
);
686 em28xx_write_reg_bits(dev
, led
->gpio_reg
,
687 (!start
^ led
->inverted
) ?
688 ~led
->gpio_mask
: led
->gpio_mask
,
694 int em28xx_gpio_set(struct em28xx
*dev
, struct em28xx_reg_seq
*gpio
)
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);
706 em28xx_write_reg(dev
, EM28XX_R12_VINENABLE
, 0x37);
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
,
727 EXPORT_SYMBOL_GPL(em28xx_gpio_set
);
729 int em28xx_set_mode(struct em28xx
*dev
, enum em28xx_mode set_mode
)
731 if (dev
->mode
== set_mode
)
734 if (set_mode
== EM28XX_SUSPEND
) {
735 dev
->mode
= set_mode
;
737 /* FIXME: add suspend support for ac97 */
739 return em28xx_gpio_set(dev
, dev
->board
.suspend_gpio
);
742 dev
->mode
= set_mode
;
744 if (dev
->mode
== EM28XX_DIGITAL_MODE
)
745 return em28xx_gpio_set(dev
, dev
->board
.dvb_gpio
);
747 return em28xx_gpio_set(dev
, INPUT(dev
->ctl_input
)->gpio
);
749 EXPORT_SYMBOL_GPL(em28xx_set_mode
);
751 /* ------------------------------------------------------------------
753 ------------------------------------------------------------------*/
756 * URB completion handler for isoc/bulk transfers
758 static void em28xx_irq_callback(struct urb
*urb
)
760 struct em28xx
*dev
= urb
->context
;
763 switch (urb
->status
) {
764 case 0: /* success */
765 case -ETIMEDOUT
: /* NAK */
767 case -ECONNRESET
: /* kill */
772 em28xx_isocdbg("urb completition error %d.\n", urb
->status
);
776 /* Copy data from URB */
777 spin_lock(&dev
->slock
);
778 dev
->usb_ctl
.urb_data_copy(dev
, urb
);
779 spin_unlock(&dev
->slock
);
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;
789 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
791 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
797 * Stop and Deallocate URBs
799 void em28xx_uninit_usb_xfer(struct em28xx
*dev
, enum em28xx_mode mode
)
802 struct em28xx_usb_bufs
*usb_bufs
;
803 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
806 em28xx_isocdbg("em28xx: called em28xx_uninit_usb_xfer in mode %d\n",
809 if (mode
== EM28XX_DIGITAL_MODE
)
810 usb_bufs
= &dev
->usb_ctl
.digital_bufs
;
812 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
814 for (i
= 0; i
< usb_bufs
->num_bufs
; i
++) {
815 urb
= usb_bufs
->urb
[i
];
817 if (!irqs_disabled())
822 if (usb_bufs
->transfer_buffer
[i
]) {
823 usb_free_coherent(udev
,
824 urb
->transfer_buffer_length
,
825 usb_bufs
->transfer_buffer
[i
],
829 usb_bufs
->urb
[i
] = NULL
;
831 usb_bufs
->transfer_buffer
[i
] = NULL
;
834 kfree(usb_bufs
->urb
);
835 kfree(usb_bufs
->transfer_buffer
);
837 usb_bufs
->urb
= NULL
;
838 usb_bufs
->transfer_buffer
= NULL
;
839 usb_bufs
->num_bufs
= 0;
841 em28xx_capture_start(dev
, 0);
843 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer
);
848 void em28xx_stop_urbs(struct em28xx
*dev
)
852 struct em28xx_usb_bufs
*isoc_bufs
= &dev
->usb_ctl
.digital_bufs
;
854 em28xx_isocdbg("em28xx: called em28xx_stop_urbs\n");
856 for (i
= 0; i
< isoc_bufs
->num_bufs
; i
++) {
857 urb
= isoc_bufs
->urb
[i
];
859 if (!irqs_disabled())
866 em28xx_capture_start(dev
, 0);
868 EXPORT_SYMBOL_GPL(em28xx_stop_urbs
);
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
)
876 struct em28xx_usb_bufs
*usb_bufs
;
878 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
883 em28xx_isocdbg("em28xx: called em28xx_alloc_isoc in mode %d\n", mode
);
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",
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",
904 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
906 dev_err(&dev
->intf
->dev
, "invalid mode selected\n");
910 /* De-allocates all pending stuff */
911 em28xx_uninit_usb_xfer(dev
, mode
);
913 usb_bufs
->num_bufs
= num_bufs
;
915 usb_bufs
->urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
919 usb_bufs
->transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
921 if (!usb_bufs
->transfer_buffer
) {
922 kfree(usb_bufs
->urb
);
926 usb_bufs
->max_pkt_size
= max_pkt_size
;
928 usb_bufs
->num_packets
= 0;
930 usb_bufs
->num_packets
= packet_multiplier
;
931 dev
->usb_ctl
.vid_buf
= NULL
;
932 dev
->usb_ctl
.vbi_buf
= NULL
;
934 sb_size
= packet_multiplier
* usb_bufs
->max_pkt_size
;
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
);
940 em28xx_uninit_usb_xfer(dev
, mode
);
943 usb_bufs
->urb
[i
] = urb
;
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",
951 in_interrupt() ? " while in int" : "");
952 em28xx_uninit_usb_xfer(dev
, mode
);
955 memset(usb_bufs
->transfer_buffer
[i
], 0, sb_size
);
957 if (xfer_bulk
) { /* bulk */
958 pipe
= usb_rcvbulkpipe(udev
,
959 mode
== EM28XX_ANALOG_MODE
?
960 dev
->analog_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
;
967 pipe
= usb_rcvisocpipe(udev
,
968 mode
== EM28XX_ANALOG_MODE
?
969 dev
->analog_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
;
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 em28xx_init_usb_xfer in mode %d\n",
1011 dev
->usb_ctl
.urb_data_copy
= urb_data_copy
;
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 */
1018 usb_bufs
= &dev
->usb_ctl
.analog_bufs
;
1023 rc
= em28xx_alloc_urbs(dev
, mode
, xfer_bulk
, num_bufs
,
1024 max_pkt_size
, packet_multiplier
);
1030 rc
= usb_clear_halt(udev
, usb_bufs
->urb
[0]->pipe
);
1032 dev_err(&dev
->intf
->dev
,
1033 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1035 em28xx_uninit_usb_xfer(dev
, mode
);
1040 init_waitqueue_head(&dma_q
->wq
);
1041 init_waitqueue_head(&vbi_dma_q
->wq
);
1043 em28xx_capture_start(dev
, 1);
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
);
1049 dev_err(&dev
->intf
->dev
,
1050 "submit of urb %i failed (error=%i)\n", i
, rc
);
1051 em28xx_uninit_usb_xfer(dev
, mode
);
1058 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer
);
1061 * Device control list
1064 static LIST_HEAD(em28xx_devlist
);
1065 static DEFINE_MUTEX(em28xx_devlist_mutex
);
1068 * Extension interface
1071 static LIST_HEAD(em28xx_extension_devlist
);
1073 int em28xx_register_extension(struct em28xx_ops
*ops
)
1075 struct em28xx
*dev
= NULL
;
1077 mutex_lock(&em28xx_devlist_mutex
);
1078 list_add_tail(&ops
->next
, &em28xx_extension_devlist
);
1079 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1082 mutex_unlock(&em28xx_devlist_mutex
);
1083 pr_info("em28xx: Registered (%s) extension\n", ops
->name
);
1086 EXPORT_SYMBOL(em28xx_register_extension
);
1088 void em28xx_unregister_extension(struct em28xx_ops
*ops
)
1090 struct em28xx
*dev
= NULL
;
1092 mutex_lock(&em28xx_devlist_mutex
);
1093 list_for_each_entry(dev
, &em28xx_devlist
, devlist
) {
1096 list_del(&ops
->next
);
1097 mutex_unlock(&em28xx_devlist_mutex
);
1098 pr_info("em28xx: Removed (%s) extension\n", ops
->name
);
1100 EXPORT_SYMBOL(em28xx_unregister_extension
);
1102 void em28xx_init_extension(struct em28xx
*dev
)
1104 const struct em28xx_ops
*ops
= NULL
;
1106 mutex_lock(&em28xx_devlist_mutex
);
1107 list_add_tail(&dev
->devlist
, &em28xx_devlist
);
1108 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1112 mutex_unlock(&em28xx_devlist_mutex
);
1115 void em28xx_close_extension(struct em28xx
*dev
)
1117 const struct em28xx_ops
*ops
= NULL
;
1119 mutex_lock(&em28xx_devlist_mutex
);
1120 list_for_each_entry(ops
, &em28xx_extension_devlist
, next
) {
1124 list_del(&dev
->devlist
);
1125 mutex_unlock(&em28xx_devlist_mutex
);
1128 int em28xx_suspend_extension(struct em28xx
*dev
)
1130 const struct em28xx_ops
*ops
= NULL
;
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
) {
1138 mutex_unlock(&em28xx_devlist_mutex
);
1142 int em28xx_resume_extension(struct em28xx
*dev
)
1144 const struct em28xx_ops
*ops
= NULL
;
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
) {
1152 mutex_unlock(&em28xx_devlist_mutex
);