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, 1000);
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, 1000);
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
702 led = em28xx_find_led(dev, EM28XX_LED_DIGITAL_CAPTURING);
703
704 if (led)
705 em28xx_write_reg_bits(dev, led->gpio_reg,
706 (!start ^ led->inverted) ?
707 ~led->gpio_mask : led->gpio_mask,
708 led->gpio_mask);
709
710 return rc;
711 }
712
em28xx_gpio_set(struct em28xx * dev,const struct em28xx_reg_seq * gpio)713 int em28xx_gpio_set(struct em28xx *dev, const struct em28xx_reg_seq *gpio)
714 {
715 int rc = 0;
716
717 if (!gpio)
718 return rc;
719
720 if (dev->mode != EM28XX_SUSPEND) {
721 em28xx_write_reg(dev, 0x48, 0x00);
722 if (dev->mode == EM28XX_ANALOG_MODE)
723 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x67);
724 else
725 em28xx_write_reg(dev, EM28XX_R12_VINENABLE, 0x37);
726 usleep_range(10000, 11000);
727 }
728
729 /* Send GPIO reset sequences specified at board entry */
730 while (gpio->sleep >= 0) {
731 if (gpio->reg >= 0) {
732 rc = em28xx_write_reg_bits(dev,
733 gpio->reg,
734 gpio->val,
735 gpio->mask);
736 if (rc < 0)
737 return rc;
738 }
739 if (gpio->sleep > 0)
740 msleep(gpio->sleep);
741
742 gpio++;
743 }
744 return rc;
745 }
746 EXPORT_SYMBOL_GPL(em28xx_gpio_set);
747
em28xx_set_mode(struct em28xx * dev,enum em28xx_mode set_mode)748 int em28xx_set_mode(struct em28xx *dev, enum em28xx_mode set_mode)
749 {
750 if (dev->mode == set_mode)
751 return 0;
752
753 if (set_mode == EM28XX_SUSPEND) {
754 dev->mode = set_mode;
755
756 /* FIXME: add suspend support for ac97 */
757
758 return em28xx_gpio_set(dev, dev->board.suspend_gpio);
759 }
760
761 dev->mode = set_mode;
762
763 if (dev->mode == EM28XX_DIGITAL_MODE)
764 return em28xx_gpio_set(dev, dev->board.dvb_gpio);
765 else
766 return em28xx_gpio_set(dev, INPUT(dev->ctl_input)->gpio);
767 }
768 EXPORT_SYMBOL_GPL(em28xx_set_mode);
769
770 /*
771 *URB control
772 */
773
774 /*
775 * URB completion handler for isoc/bulk transfers
776 */
em28xx_irq_callback(struct urb * urb)777 static void em28xx_irq_callback(struct urb *urb)
778 {
779 struct em28xx *dev = urb->context;
780 unsigned long flags;
781 int i;
782
783 switch (urb->status) {
784 case 0: /* success */
785 case -ETIMEDOUT: /* NAK */
786 break;
787 case -ECONNRESET: /* kill */
788 case -ENOENT:
789 case -ESHUTDOWN:
790 return;
791 default: /* error */
792 em28xx_isocdbg("urb completion error %d.\n", urb->status);
793 break;
794 }
795
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);
800
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;
806 }
807 urb->status = 0;
808
809 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
810 if (urb->status) {
811 em28xx_isocdbg("urb resubmit failed (error=%i)\n",
812 urb->status);
813 }
814 }
815
816 /*
817 * Stop and Deallocate URBs
818 */
em28xx_uninit_usb_xfer(struct em28xx * dev,enum em28xx_mode mode)819 void em28xx_uninit_usb_xfer(struct em28xx *dev, enum em28xx_mode mode)
820 {
821 struct urb *urb;
822 struct em28xx_usb_bufs *usb_bufs;
823 int i;
824
825 em28xx_isocdbg("called %s in mode %d\n", __func__, mode);
826
827 if (mode == EM28XX_DIGITAL_MODE)
828 usb_bufs = &dev->usb_ctl.digital_bufs;
829 else
830 usb_bufs = &dev->usb_ctl.analog_bufs;
831
832 for (i = 0; i < usb_bufs->num_bufs; i++) {
833 urb = usb_bufs->urb[i];
834 if (urb) {
835 if (!irqs_disabled())
836 usb_kill_urb(urb);
837 else
838 usb_unlink_urb(urb);
839
840 usb_free_urb(urb);
841 usb_bufs->urb[i] = NULL;
842 }
843 }
844
845 kfree(usb_bufs->urb);
846 kfree(usb_bufs->buf);
847
848 usb_bufs->urb = NULL;
849 usb_bufs->buf = NULL;
850 usb_bufs->num_bufs = 0;
851
852 em28xx_capture_start(dev, 0);
853 }
854 EXPORT_SYMBOL_GPL(em28xx_uninit_usb_xfer);
855
856 /*
857 * Stop URBs
858 */
em28xx_stop_urbs(struct em28xx * dev)859 void em28xx_stop_urbs(struct em28xx *dev)
860 {
861 int i;
862 struct urb *urb;
863 struct em28xx_usb_bufs *isoc_bufs = &dev->usb_ctl.digital_bufs;
864
865 em28xx_isocdbg("called %s\n", __func__);
866
867 for (i = 0; i < isoc_bufs->num_bufs; i++) {
868 urb = isoc_bufs->urb[i];
869 if (urb) {
870 if (!irqs_disabled())
871 usb_kill_urb(urb);
872 else
873 usb_unlink_urb(urb);
874 }
875 }
876
877 em28xx_capture_start(dev, 0);
878 }
879 EXPORT_SYMBOL_GPL(em28xx_stop_urbs);
880
881 /*
882 * Allocate URBs
883 */
em28xx_alloc_urbs(struct em28xx * dev,enum em28xx_mode mode,int xfer_bulk,int num_bufs,int max_pkt_size,int packet_multiplier)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)
886 {
887 struct em28xx_usb_bufs *usb_bufs;
888 struct urb *urb;
889 struct usb_device *udev = interface_to_usbdev(dev->intf);
890 int i;
891 int sb_size, pipe;
892 int j, k;
893
894 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__, mode);
895
896 /*
897 * Check mode and if we have an endpoint for the selected
898 * transfer type, select buffer
899 */
900 if (mode == EM28XX_DIGITAL_MODE) {
901 if ((xfer_bulk && !dev->dvb_ep_bulk) ||
902 (!xfer_bulk && !dev->dvb_ep_isoc)) {
903 dev_err(&dev->intf->dev,
904 "no endpoint for DVB mode and transfer type %d\n",
905 xfer_bulk > 0);
906 return -EINVAL;
907 }
908 usb_bufs = &dev->usb_ctl.digital_bufs;
909 } else if (mode == EM28XX_ANALOG_MODE) {
910 if ((xfer_bulk && !dev->analog_ep_bulk) ||
911 (!xfer_bulk && !dev->analog_ep_isoc)) {
912 dev_err(&dev->intf->dev,
913 "no endpoint for analog mode and transfer type %d\n",
914 xfer_bulk > 0);
915 return -EINVAL;
916 }
917 usb_bufs = &dev->usb_ctl.analog_bufs;
918 } else {
919 dev_err(&dev->intf->dev, "invalid mode selected\n");
920 return -EINVAL;
921 }
922
923 /* De-allocates all pending stuff */
924 em28xx_uninit_usb_xfer(dev, mode);
925
926 usb_bufs->num_bufs = num_bufs;
927
928 usb_bufs->urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
929 if (!usb_bufs->urb)
930 return -ENOMEM;
931
932 usb_bufs->buf = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
933 if (!usb_bufs->buf) {
934 kfree(usb_bufs->urb);
935 return -ENOMEM;
936 }
937
938 usb_bufs->max_pkt_size = max_pkt_size;
939 if (xfer_bulk)
940 usb_bufs->num_packets = 0;
941 else
942 usb_bufs->num_packets = packet_multiplier;
943 dev->usb_ctl.vid_buf = NULL;
944 dev->usb_ctl.vbi_buf = NULL;
945
946 sb_size = packet_multiplier * usb_bufs->max_pkt_size;
947
948 /* allocate urbs and transfer buffers */
949 for (i = 0; i < usb_bufs->num_bufs; i++) {
950 urb = usb_alloc_urb(usb_bufs->num_packets, GFP_KERNEL);
951 if (!urb) {
952 em28xx_uninit_usb_xfer(dev, mode);
953 return -ENOMEM;
954 }
955 usb_bufs->urb[i] = urb;
956
957 usb_bufs->buf[i] = kzalloc(sb_size, GFP_KERNEL);
958 if (!usb_bufs->buf[i]) {
959 for (i--; i >= 0; i--)
960 kfree(usb_bufs->buf[i]);
961
962 em28xx_uninit_usb_xfer(dev, mode);
963 return -ENOMEM;
964 }
965
966 urb->transfer_flags = URB_FREE_BUFFER;
967
968 if (xfer_bulk) { /* bulk */
969 pipe = usb_rcvbulkpipe(udev,
970 mode == EM28XX_ANALOG_MODE ?
971 dev->analog_ep_bulk :
972 dev->dvb_ep_bulk);
973 usb_fill_bulk_urb(urb, udev, pipe, usb_bufs->buf[i],
974 sb_size, em28xx_irq_callback, dev);
975 } else { /* isoc */
976 pipe = usb_rcvisocpipe(udev,
977 mode == EM28XX_ANALOG_MODE ?
978 dev->analog_ep_isoc :
979 dev->dvb_ep_isoc);
980 usb_fill_int_urb(urb, udev, pipe, usb_bufs->buf[i],
981 sb_size, em28xx_irq_callback, dev, 1);
982 urb->transfer_flags |= URB_ISO_ASAP;
983 k = 0;
984 for (j = 0; j < usb_bufs->num_packets; j++) {
985 urb->iso_frame_desc[j].offset = k;
986 urb->iso_frame_desc[j].length =
987 usb_bufs->max_pkt_size;
988 k += usb_bufs->max_pkt_size;
989 }
990 }
991
992 urb->number_of_packets = usb_bufs->num_packets;
993 }
994
995 return 0;
996 }
997 EXPORT_SYMBOL_GPL(em28xx_alloc_urbs);
998
999 /*
1000 * Allocate URBs and start IRQ
1001 */
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))1002 int em28xx_init_usb_xfer(struct em28xx *dev, enum em28xx_mode mode,
1003 int xfer_bulk, int num_bufs, int max_pkt_size,
1004 int packet_multiplier,
1005 int (*urb_data_copy)(struct em28xx *dev, struct urb *urb))
1006 {
1007 struct em28xx_dmaqueue *dma_q = &dev->vidq;
1008 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
1009 struct em28xx_usb_bufs *usb_bufs;
1010 struct usb_device *udev = interface_to_usbdev(dev->intf);
1011 int i;
1012 int rc;
1013 int alloc;
1014
1015 em28xx_isocdbg("em28xx: called %s in mode %d\n", __func__, mode);
1016
1017 dev->usb_ctl.urb_data_copy = urb_data_copy;
1018
1019 if (mode == EM28XX_DIGITAL_MODE) {
1020 usb_bufs = &dev->usb_ctl.digital_bufs;
1021 /* no need to free/alloc usb buffers in digital mode */
1022 alloc = 0;
1023 } else {
1024 usb_bufs = &dev->usb_ctl.analog_bufs;
1025 alloc = 1;
1026 }
1027
1028 if (alloc) {
1029 rc = em28xx_alloc_urbs(dev, mode, xfer_bulk, num_bufs,
1030 max_pkt_size, packet_multiplier);
1031 if (rc)
1032 return rc;
1033 }
1034
1035 if (xfer_bulk) {
1036 rc = usb_clear_halt(udev, usb_bufs->urb[0]->pipe);
1037 if (rc < 0) {
1038 dev_err(&dev->intf->dev,
1039 "failed to clear USB bulk endpoint stall/halt condition (error=%i)\n",
1040 rc);
1041 em28xx_uninit_usb_xfer(dev, mode);
1042 return rc;
1043 }
1044 }
1045
1046 init_waitqueue_head(&dma_q->wq);
1047 init_waitqueue_head(&vbi_dma_q->wq);
1048
1049 em28xx_capture_start(dev, 1);
1050
1051 /* submit urbs and enables IRQ */
1052 for (i = 0; i < usb_bufs->num_bufs; i++) {
1053 rc = usb_submit_urb(usb_bufs->urb[i], GFP_KERNEL);
1054 if (rc) {
1055 dev_err(&dev->intf->dev,
1056 "submit of urb %i failed (error=%i)\n", i, rc);
1057 em28xx_uninit_usb_xfer(dev, mode);
1058 return rc;
1059 }
1060 }
1061
1062 return 0;
1063 }
1064 EXPORT_SYMBOL_GPL(em28xx_init_usb_xfer);
1065
1066 /*
1067 * Device control list
1068 */
1069
1070 static LIST_HEAD(em28xx_devlist);
1071 static DEFINE_MUTEX(em28xx_devlist_mutex);
1072
1073 /*
1074 * Extension interface
1075 */
1076
1077 static LIST_HEAD(em28xx_extension_devlist);
1078
em28xx_register_extension(struct em28xx_ops * ops)1079 int em28xx_register_extension(struct em28xx_ops *ops)
1080 {
1081 struct em28xx *dev = NULL;
1082
1083 mutex_lock(&em28xx_devlist_mutex);
1084 list_add_tail(&ops->next, &em28xx_extension_devlist);
1085 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1086 if (ops->init) {
1087 ops->init(dev);
1088 if (dev->dev_next)
1089 ops->init(dev->dev_next);
1090 }
1091 }
1092 mutex_unlock(&em28xx_devlist_mutex);
1093 pr_info("em28xx: Registered (%s) extension\n", ops->name);
1094 return 0;
1095 }
1096 EXPORT_SYMBOL(em28xx_register_extension);
1097
em28xx_unregister_extension(struct em28xx_ops * ops)1098 void em28xx_unregister_extension(struct em28xx_ops *ops)
1099 {
1100 struct em28xx *dev = NULL;
1101
1102 mutex_lock(&em28xx_devlist_mutex);
1103 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1104 if (ops->fini) {
1105 if (dev->dev_next)
1106 ops->fini(dev->dev_next);
1107 ops->fini(dev);
1108 }
1109 }
1110 list_del(&ops->next);
1111 mutex_unlock(&em28xx_devlist_mutex);
1112 pr_info("em28xx: Removed (%s) extension\n", ops->name);
1113 }
1114 EXPORT_SYMBOL(em28xx_unregister_extension);
1115
em28xx_init_extension(struct em28xx * dev)1116 void em28xx_init_extension(struct em28xx *dev)
1117 {
1118 const struct em28xx_ops *ops = NULL;
1119
1120 mutex_lock(&em28xx_devlist_mutex);
1121 list_add_tail(&dev->devlist, &em28xx_devlist);
1122 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1123 if (ops->init) {
1124 ops->init(dev);
1125 if (dev->dev_next)
1126 ops->init(dev->dev_next);
1127 }
1128 }
1129 mutex_unlock(&em28xx_devlist_mutex);
1130 }
1131
em28xx_close_extension(struct em28xx * dev)1132 void em28xx_close_extension(struct em28xx *dev)
1133 {
1134 const struct em28xx_ops *ops = NULL;
1135
1136 mutex_lock(&em28xx_devlist_mutex);
1137 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1138 if (ops->fini) {
1139 if (dev->dev_next)
1140 ops->fini(dev->dev_next);
1141 ops->fini(dev);
1142 }
1143 }
1144 list_del(&dev->devlist);
1145 mutex_unlock(&em28xx_devlist_mutex);
1146 }
1147
em28xx_suspend_extension(struct em28xx * dev)1148 int em28xx_suspend_extension(struct em28xx *dev)
1149 {
1150 const struct em28xx_ops *ops = NULL;
1151
1152 dev_info(&dev->intf->dev, "Suspending extensions\n");
1153 mutex_lock(&em28xx_devlist_mutex);
1154 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1155 if (!ops->suspend)
1156 continue;
1157 ops->suspend(dev);
1158 if (dev->dev_next)
1159 ops->suspend(dev->dev_next);
1160 }
1161 mutex_unlock(&em28xx_devlist_mutex);
1162 return 0;
1163 }
1164
em28xx_resume_extension(struct em28xx * dev)1165 int em28xx_resume_extension(struct em28xx *dev)
1166 {
1167 const struct em28xx_ops *ops = NULL;
1168
1169 dev_info(&dev->intf->dev, "Resuming extensions\n");
1170 mutex_lock(&em28xx_devlist_mutex);
1171 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1172 if (!ops->resume)
1173 continue;
1174 ops->resume(dev);
1175 if (dev->dev_next)
1176 ops->resume(dev->dev_next);
1177 }
1178 mutex_unlock(&em28xx_devlist_mutex);
1179 return 0;
1180 }
1181