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