em28xx-video.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
  3. 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. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  9. by Luca Risolia <luca.risolia@studio.unibo.it>
  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.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/i2c.h>
  29. #include <linux/version.h>
  30. #include <linux/mm.h>
  31. #include <linux/mutex.h>
  32. #include "em28xx.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/msp3400.h>
  37. #include <media/tuner.h>
  38. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  39. "Markus Rechberger <mrechberger@gmail.com>, " \
  40. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  41. "Sascha Sommer <saschasommer@freenet.de>"
  42. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  43. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 1)
  44. #define em28xx_videodbg(fmt, arg...) do {\
  45. if (video_debug) \
  46. printk(KERN_INFO "%s %s :"fmt, \
  47. dev->name, __func__ , ##arg); } while (0)
  48. static unsigned int isoc_debug;
  49. module_param(isoc_debug, int, 0644);
  50. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  51. #define em28xx_isocdbg(fmt, arg...) \
  52. do {\
  53. if (isoc_debug) { \
  54. printk(KERN_INFO "%s %s :"fmt, \
  55. dev->name, __func__ , ##arg); \
  56. } \
  57. } while (0)
  58. MODULE_AUTHOR(DRIVER_AUTHOR);
  59. MODULE_DESCRIPTION(DRIVER_DESC);
  60. MODULE_LICENSE("GPL");
  61. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. module_param_array(video_nr, int, NULL, 0444);
  65. module_param_array(vbi_nr, int, NULL, 0444);
  66. module_param_array(radio_nr, int, NULL, 0444);
  67. MODULE_PARM_DESC(video_nr, "video device numbers");
  68. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  69. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  70. static unsigned int video_debug;
  71. module_param(video_debug, int, 0644);
  72. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  73. /* supported video standards */
  74. static struct em28xx_fmt format[] = {
  75. {
  76. .name = "16bpp YUY2, 4:2:2, packed",
  77. .fourcc = V4L2_PIX_FMT_YUYV,
  78. .depth = 16,
  79. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  80. },
  81. };
  82. /* supported controls */
  83. /* Common to all boards */
  84. static struct v4l2_queryctrl em28xx_qctrl[] = {
  85. {
  86. .id = V4L2_CID_AUDIO_VOLUME,
  87. .type = V4L2_CTRL_TYPE_INTEGER,
  88. .name = "Volume",
  89. .minimum = 0x0,
  90. .maximum = 0x1f,
  91. .step = 0x1,
  92. .default_value = 0x1f,
  93. .flags = 0,
  94. }, {
  95. .id = V4L2_CID_AUDIO_MUTE,
  96. .type = V4L2_CTRL_TYPE_BOOLEAN,
  97. .name = "Mute",
  98. .minimum = 0,
  99. .maximum = 1,
  100. .step = 1,
  101. .default_value = 1,
  102. .flags = 0,
  103. }
  104. };
  105. /* ------------------------------------------------------------------
  106. DMA and thread functions
  107. ------------------------------------------------------------------*/
  108. /*
  109. * Announces that a buffer were filled and request the next
  110. */
  111. static inline void buffer_filled(struct em28xx *dev,
  112. struct em28xx_dmaqueue *dma_q,
  113. struct em28xx_buffer *buf)
  114. {
  115. /* Advice that buffer was filled */
  116. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  117. buf->vb.state = VIDEOBUF_DONE;
  118. buf->vb.field_count++;
  119. do_gettimeofday(&buf->vb.ts);
  120. dev->isoc_ctl.buf = NULL;
  121. list_del(&buf->vb.queue);
  122. wake_up(&buf->vb.done);
  123. }
  124. /*
  125. * Identify the buffer header type and properly handles
  126. */
  127. static void em28xx_copy_video(struct em28xx *dev,
  128. struct em28xx_dmaqueue *dma_q,
  129. struct em28xx_buffer *buf,
  130. unsigned char *p,
  131. unsigned char *outp, unsigned long len)
  132. {
  133. void *fieldstart, *startwrite, *startread;
  134. int linesdone, currlinedone, offset, lencopy, remain;
  135. int bytesperline = dev->width << 1;
  136. if (dma_q->pos + len > buf->vb.size)
  137. len = buf->vb.size - dma_q->pos;
  138. if (p[0] != 0x88 && p[0] != 0x22) {
  139. em28xx_isocdbg("frame is not complete\n");
  140. len += 4;
  141. } else
  142. p += 4;
  143. startread = p;
  144. remain = len;
  145. /* Interlaces frame */
  146. if (buf->top_field)
  147. fieldstart = outp;
  148. else
  149. fieldstart = outp + bytesperline;
  150. linesdone = dma_q->pos / bytesperline;
  151. currlinedone = dma_q->pos % bytesperline;
  152. offset = linesdone * bytesperline * 2 + currlinedone;
  153. startwrite = fieldstart + offset;
  154. lencopy = bytesperline - currlinedone;
  155. lencopy = lencopy > remain ? remain : lencopy;
  156. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  157. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  158. ((char *)startwrite + lencopy) -
  159. ((char *)outp + buf->vb.size));
  160. lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
  161. }
  162. if (lencopy <= 0)
  163. return;
  164. memcpy(startwrite, startread, lencopy);
  165. remain -= lencopy;
  166. while (remain > 0) {
  167. startwrite += lencopy + bytesperline;
  168. startread += lencopy;
  169. if (bytesperline > remain)
  170. lencopy = remain;
  171. else
  172. lencopy = bytesperline;
  173. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  174. em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
  175. ((char *)startwrite + lencopy) -
  176. ((char *)outp + buf->vb.size));
  177. lencopy = remain = (char *)outp + buf->vb.size -
  178. (char *)startwrite;
  179. }
  180. if (lencopy <= 0)
  181. break;
  182. memcpy(startwrite, startread, lencopy);
  183. remain -= lencopy;
  184. }
  185. dma_q->pos += len;
  186. }
  187. static inline void print_err_status(struct em28xx *dev,
  188. int packet, int status)
  189. {
  190. char *errmsg = "Unknown";
  191. switch (status) {
  192. case -ENOENT:
  193. errmsg = "unlinked synchronuously";
  194. break;
  195. case -ECONNRESET:
  196. errmsg = "unlinked asynchronuously";
  197. break;
  198. case -ENOSR:
  199. errmsg = "Buffer error (overrun)";
  200. break;
  201. case -EPIPE:
  202. errmsg = "Stalled (device not responding)";
  203. break;
  204. case -EOVERFLOW:
  205. errmsg = "Babble (bad cable?)";
  206. break;
  207. case -EPROTO:
  208. errmsg = "Bit-stuff error (bad cable?)";
  209. break;
  210. case -EILSEQ:
  211. errmsg = "CRC/Timeout (could be anything)";
  212. break;
  213. case -ETIME:
  214. errmsg = "Device does not respond";
  215. break;
  216. }
  217. if (packet < 0) {
  218. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  219. } else {
  220. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  221. packet, status, errmsg);
  222. }
  223. }
  224. /*
  225. * video-buf generic routine to get the next available buffer
  226. */
  227. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  228. struct em28xx_buffer **buf)
  229. {
  230. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  231. char *outp;
  232. if (list_empty(&dma_q->active)) {
  233. em28xx_isocdbg("No active queue to serve\n");
  234. dev->isoc_ctl.buf = NULL;
  235. *buf = NULL;
  236. return;
  237. }
  238. /* Get the next buffer */
  239. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  240. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  241. outp = videobuf_to_vmalloc(&(*buf)->vb);
  242. memset(outp, 0, (*buf)->vb.size);
  243. dev->isoc_ctl.buf = *buf;
  244. return;
  245. }
  246. /*
  247. * Controls the isoc copy of each urb packet
  248. */
  249. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  250. {
  251. struct em28xx_buffer *buf;
  252. struct em28xx_dmaqueue *dma_q = urb->context;
  253. unsigned char *outp = NULL;
  254. int i, len = 0, rc = 1;
  255. unsigned char *p;
  256. if (!dev)
  257. return 0;
  258. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  259. return 0;
  260. if (urb->status < 0) {
  261. print_err_status(dev, -1, urb->status);
  262. if (urb->status == -ENOENT)
  263. return 0;
  264. }
  265. buf = dev->isoc_ctl.buf;
  266. if (buf != NULL)
  267. outp = videobuf_to_vmalloc(&buf->vb);
  268. for (i = 0; i < urb->number_of_packets; i++) {
  269. int status = urb->iso_frame_desc[i].status;
  270. if (status < 0) {
  271. print_err_status(dev, i, status);
  272. if (urb->iso_frame_desc[i].status != -EPROTO)
  273. continue;
  274. }
  275. len = urb->iso_frame_desc[i].actual_length - 4;
  276. if (urb->iso_frame_desc[i].actual_length <= 0) {
  277. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  278. continue;
  279. }
  280. if (urb->iso_frame_desc[i].actual_length >
  281. dev->max_pkt_size) {
  282. em28xx_isocdbg("packet bigger than packet size");
  283. continue;
  284. }
  285. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  286. /* FIXME: incomplete buffer checks where removed to make
  287. logic simpler. Impacts of those changes should be evaluated
  288. */
  289. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  290. em28xx_isocdbg("VBI HEADER!!!\n");
  291. /* FIXME: Should add vbi copy */
  292. continue;
  293. }
  294. if (p[0] == 0x22 && p[1] == 0x5a) {
  295. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  296. len, (p[2] & 1)? "odd" : "even");
  297. if (!(p[2] & 1)) {
  298. if (buf != NULL)
  299. buffer_filled(dev, dma_q, buf);
  300. get_next_buf(dma_q, &buf);
  301. if (buf == NULL)
  302. outp = NULL;
  303. else
  304. outp = videobuf_to_vmalloc(&buf->vb);
  305. }
  306. if (buf != NULL) {
  307. if (p[2] & 1)
  308. buf->top_field = 0;
  309. else
  310. buf->top_field = 1;
  311. }
  312. dma_q->pos = 0;
  313. }
  314. if (buf != NULL)
  315. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  316. }
  317. return rc;
  318. }
  319. /* ------------------------------------------------------------------
  320. Videobuf operations
  321. ------------------------------------------------------------------*/
  322. static int
  323. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  324. {
  325. struct em28xx_fh *fh = vq->priv_data;
  326. struct em28xx *dev = fh->dev;
  327. struct v4l2_frequency f;
  328. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  329. if (0 == *count)
  330. *count = EM28XX_DEF_BUF;
  331. if (*count < EM28XX_MIN_BUF)
  332. *count = EM28XX_MIN_BUF;
  333. /* Ask tuner to go to analog or radio mode */
  334. memset(&f, 0, sizeof(f));
  335. f.frequency = dev->ctl_freq;
  336. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  337. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
  338. return 0;
  339. }
  340. /* This is called *without* dev->slock held; please keep it that way */
  341. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  342. {
  343. struct em28xx_fh *fh = vq->priv_data;
  344. struct em28xx *dev = fh->dev;
  345. unsigned long flags = 0;
  346. if (in_interrupt())
  347. BUG();
  348. /* We used to wait for the buffer to finish here, but this didn't work
  349. because, as we were keeping the state as VIDEOBUF_QUEUED,
  350. videobuf_queue_cancel marked it as finished for us.
  351. (Also, it could wedge forever if the hardware was misconfigured.)
  352. This should be safe; by the time we get here, the buffer isn't
  353. queued anymore. If we ever start marking the buffers as
  354. VIDEOBUF_ACTIVE, it won't be, though.
  355. */
  356. spin_lock_irqsave(&dev->slock, flags);
  357. if (dev->isoc_ctl.buf == buf)
  358. dev->isoc_ctl.buf = NULL;
  359. spin_unlock_irqrestore(&dev->slock, flags);
  360. videobuf_vmalloc_free(&buf->vb);
  361. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  362. }
  363. static int
  364. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  365. enum v4l2_field field)
  366. {
  367. struct em28xx_fh *fh = vq->priv_data;
  368. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  369. struct em28xx *dev = fh->dev;
  370. int rc = 0, urb_init = 0;
  371. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  372. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  373. return -EINVAL;
  374. buf->vb.width = dev->width;
  375. buf->vb.height = dev->height;
  376. buf->vb.field = field;
  377. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  378. rc = videobuf_iolock(vq, &buf->vb, NULL);
  379. if (rc < 0)
  380. goto fail;
  381. }
  382. if (!dev->isoc_ctl.num_bufs)
  383. urb_init = 1;
  384. if (urb_init) {
  385. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  386. EM28XX_NUM_BUFS, dev->max_pkt_size,
  387. em28xx_isoc_copy);
  388. if (rc < 0)
  389. goto fail;
  390. }
  391. buf->vb.state = VIDEOBUF_PREPARED;
  392. return 0;
  393. fail:
  394. free_buffer(vq, buf);
  395. return rc;
  396. }
  397. static void
  398. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  399. {
  400. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  401. struct em28xx_fh *fh = vq->priv_data;
  402. struct em28xx *dev = fh->dev;
  403. struct em28xx_dmaqueue *vidq = &dev->vidq;
  404. buf->vb.state = VIDEOBUF_QUEUED;
  405. list_add_tail(&buf->vb.queue, &vidq->active);
  406. }
  407. static void buffer_release(struct videobuf_queue *vq,
  408. struct videobuf_buffer *vb)
  409. {
  410. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  411. struct em28xx_fh *fh = vq->priv_data;
  412. struct em28xx *dev = (struct em28xx *)fh->dev;
  413. em28xx_isocdbg("em28xx: called buffer_release\n");
  414. free_buffer(vq, buf);
  415. }
  416. static struct videobuf_queue_ops em28xx_video_qops = {
  417. .buf_setup = buffer_setup,
  418. .buf_prepare = buffer_prepare,
  419. .buf_queue = buffer_queue,
  420. .buf_release = buffer_release,
  421. };
  422. /********************* v4l2 interface **************************************/
  423. static void video_mux(struct em28xx *dev, int index)
  424. {
  425. struct v4l2_routing route;
  426. route.input = INPUT(index)->vmux;
  427. route.output = 0;
  428. dev->ctl_input = index;
  429. dev->ctl_ainput = INPUT(index)->amux;
  430. dev->ctl_aoutput = INPUT(index)->aout;
  431. if (!dev->ctl_aoutput)
  432. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  433. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  434. if (dev->board.has_msp34xx) {
  435. if (dev->i2s_speed) {
  436. em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
  437. &dev->i2s_speed);
  438. }
  439. route.input = dev->ctl_ainput;
  440. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  441. /* Note: this is msp3400 specific */
  442. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
  443. &route);
  444. }
  445. em28xx_audio_analog_set(dev);
  446. }
  447. /* Usage lock check functions */
  448. static int res_get(struct em28xx_fh *fh)
  449. {
  450. struct em28xx *dev = fh->dev;
  451. int rc = 0;
  452. /* This instance already has stream_on */
  453. if (fh->stream_on)
  454. return rc;
  455. if (dev->stream_on)
  456. return -EBUSY;
  457. dev->stream_on = 1;
  458. fh->stream_on = 1;
  459. return rc;
  460. }
  461. static int res_check(struct em28xx_fh *fh)
  462. {
  463. return (fh->stream_on);
  464. }
  465. static void res_free(struct em28xx_fh *fh)
  466. {
  467. struct em28xx *dev = fh->dev;
  468. fh->stream_on = 0;
  469. dev->stream_on = 0;
  470. }
  471. /*
  472. * em28xx_get_ctrl()
  473. * return the current saturation, brightness or contrast, mute state
  474. */
  475. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  476. {
  477. switch (ctrl->id) {
  478. case V4L2_CID_AUDIO_MUTE:
  479. ctrl->value = dev->mute;
  480. return 0;
  481. case V4L2_CID_AUDIO_VOLUME:
  482. ctrl->value = dev->volume;
  483. return 0;
  484. default:
  485. return -EINVAL;
  486. }
  487. }
  488. /*
  489. * em28xx_set_ctrl()
  490. * mute or set new saturation, brightness or contrast
  491. */
  492. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  493. {
  494. switch (ctrl->id) {
  495. case V4L2_CID_AUDIO_MUTE:
  496. if (ctrl->value != dev->mute) {
  497. dev->mute = ctrl->value;
  498. return em28xx_audio_analog_set(dev);
  499. }
  500. return 0;
  501. case V4L2_CID_AUDIO_VOLUME:
  502. dev->volume = ctrl->value;
  503. return em28xx_audio_analog_set(dev);
  504. default:
  505. return -EINVAL;
  506. }
  507. }
  508. static int check_dev(struct em28xx *dev)
  509. {
  510. if (dev->state & DEV_DISCONNECTED) {
  511. em28xx_errdev("v4l2 ioctl: device not present\n");
  512. return -ENODEV;
  513. }
  514. if (dev->state & DEV_MISCONFIGURED) {
  515. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  516. "close and open it again\n");
  517. return -EIO;
  518. }
  519. return 0;
  520. }
  521. static void get_scale(struct em28xx *dev,
  522. unsigned int width, unsigned int height,
  523. unsigned int *hscale, unsigned int *vscale)
  524. {
  525. unsigned int maxw = norm_maxw(dev);
  526. unsigned int maxh = norm_maxh(dev);
  527. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  528. if (*hscale >= 0x4000)
  529. *hscale = 0x3fff;
  530. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  531. if (*vscale >= 0x4000)
  532. *vscale = 0x3fff;
  533. }
  534. /* ------------------------------------------------------------------
  535. IOCTL vidioc handling
  536. ------------------------------------------------------------------*/
  537. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  538. struct v4l2_format *f)
  539. {
  540. struct em28xx_fh *fh = priv;
  541. struct em28xx *dev = fh->dev;
  542. mutex_lock(&dev->lock);
  543. f->fmt.pix.width = dev->width;
  544. f->fmt.pix.height = dev->height;
  545. f->fmt.pix.pixelformat = dev->format->fourcc;
  546. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  547. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  548. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  549. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  550. f->fmt.pix.field = dev->interlaced ?
  551. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  552. mutex_unlock(&dev->lock);
  553. return 0;
  554. }
  555. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  556. {
  557. unsigned int i;
  558. for (i = 0; i < ARRAY_SIZE(format); i++)
  559. if (format[i].fourcc == fourcc)
  560. return &format[i];
  561. return NULL;
  562. }
  563. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  564. struct v4l2_format *f)
  565. {
  566. struct em28xx_fh *fh = priv;
  567. struct em28xx *dev = fh->dev;
  568. int width = f->fmt.pix.width;
  569. int height = f->fmt.pix.height;
  570. unsigned int maxw = norm_maxw(dev);
  571. unsigned int maxh = norm_maxh(dev);
  572. unsigned int hscale, vscale;
  573. struct em28xx_fmt *fmt;
  574. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  575. if (!fmt) {
  576. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  577. f->fmt.pix.pixelformat);
  578. return -EINVAL;
  579. }
  580. /* width must even because of the YUYV format
  581. height must be even because of interlacing */
  582. height &= 0xfffe;
  583. width &= 0xfffe;
  584. if (unlikely(height < 32))
  585. height = 32;
  586. if (unlikely(height > maxh))
  587. height = maxh;
  588. if (unlikely(width < 48))
  589. width = 48;
  590. if (unlikely(width > maxw))
  591. width = maxw;
  592. if (dev->board.is_em2800) {
  593. /* the em2800 can only scale down to 50% */
  594. if (height % (maxh / 2))
  595. height = maxh;
  596. if (width % (maxw / 2))
  597. width = maxw;
  598. /* according to empiatech support */
  599. /* the MaxPacketSize is to small to support */
  600. /* framesizes larger than 640x480 @ 30 fps */
  601. /* or 640x576 @ 25 fps. As this would cut */
  602. /* of a part of the image we prefer */
  603. /* 360x576 or 360x480 for now */
  604. if (width == maxw && height == maxh)
  605. width /= 2;
  606. }
  607. get_scale(dev, width, height, &hscale, &vscale);
  608. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  609. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  610. f->fmt.pix.width = width;
  611. f->fmt.pix.height = height;
  612. f->fmt.pix.pixelformat = fmt->fourcc;
  613. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  614. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  615. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  616. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  617. return 0;
  618. }
  619. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  620. struct v4l2_format *f)
  621. {
  622. struct em28xx_fh *fh = priv;
  623. struct em28xx *dev = fh->dev;
  624. int rc;
  625. struct em28xx_fmt *fmt;
  626. rc = check_dev(dev);
  627. if (rc < 0)
  628. return rc;
  629. mutex_lock(&dev->lock);
  630. vidioc_try_fmt_vid_cap(file, priv, f);
  631. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  632. if (!fmt) {
  633. rc = -EINVAL;
  634. goto out;
  635. }
  636. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  637. em28xx_errdev("%s queue busy\n", __func__);
  638. rc = -EBUSY;
  639. goto out;
  640. }
  641. if (dev->stream_on && !fh->stream_on) {
  642. em28xx_errdev("%s device in use by another fh\n", __func__);
  643. rc = -EBUSY;
  644. goto out;
  645. }
  646. /* set new image size */
  647. dev->width = f->fmt.pix.width;
  648. dev->height = f->fmt.pix.height;
  649. dev->format = fmt;
  650. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  651. em28xx_set_alternate(dev);
  652. em28xx_resolution_set(dev);
  653. rc = 0;
  654. out:
  655. mutex_unlock(&dev->lock);
  656. return rc;
  657. }
  658. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
  659. {
  660. struct em28xx_fh *fh = priv;
  661. struct em28xx *dev = fh->dev;
  662. struct v4l2_format f;
  663. int rc;
  664. rc = check_dev(dev);
  665. if (rc < 0)
  666. return rc;
  667. mutex_lock(&dev->lock);
  668. dev->norm = *norm;
  669. /* Adjusts width/height, if needed */
  670. f.fmt.pix.width = dev->width;
  671. f.fmt.pix.height = dev->height;
  672. vidioc_try_fmt_vid_cap(file, priv, &f);
  673. /* set new image size */
  674. dev->width = f.fmt.pix.width;
  675. dev->height = f.fmt.pix.height;
  676. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  677. em28xx_resolution_set(dev);
  678. em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
  679. mutex_unlock(&dev->lock);
  680. return 0;
  681. }
  682. static const char *iname[] = {
  683. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  684. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  685. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  686. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  687. [EM28XX_VMUX_SVIDEO] = "S-Video",
  688. [EM28XX_VMUX_TELEVISION] = "Television",
  689. [EM28XX_VMUX_CABLE] = "Cable TV",
  690. [EM28XX_VMUX_DVB] = "DVB",
  691. [EM28XX_VMUX_DEBUG] = "for debug only",
  692. };
  693. static int vidioc_enum_input(struct file *file, void *priv,
  694. struct v4l2_input *i)
  695. {
  696. struct em28xx_fh *fh = priv;
  697. struct em28xx *dev = fh->dev;
  698. unsigned int n;
  699. n = i->index;
  700. if (n >= MAX_EM28XX_INPUT)
  701. return -EINVAL;
  702. if (0 == INPUT(n)->type)
  703. return -EINVAL;
  704. i->index = n;
  705. i->type = V4L2_INPUT_TYPE_CAMERA;
  706. strcpy(i->name, iname[INPUT(n)->type]);
  707. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  708. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  709. i->type = V4L2_INPUT_TYPE_TUNER;
  710. i->std = dev->vdev->tvnorms;
  711. return 0;
  712. }
  713. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  714. {
  715. struct em28xx_fh *fh = priv;
  716. struct em28xx *dev = fh->dev;
  717. *i = dev->ctl_input;
  718. return 0;
  719. }
  720. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  721. {
  722. struct em28xx_fh *fh = priv;
  723. struct em28xx *dev = fh->dev;
  724. int rc;
  725. rc = check_dev(dev);
  726. if (rc < 0)
  727. return rc;
  728. if (i >= MAX_EM28XX_INPUT)
  729. return -EINVAL;
  730. if (0 == INPUT(i)->type)
  731. return -EINVAL;
  732. dev->ctl_input = i;
  733. mutex_lock(&dev->lock);
  734. video_mux(dev, dev->ctl_input);
  735. mutex_unlock(&dev->lock);
  736. return 0;
  737. }
  738. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  739. {
  740. struct em28xx_fh *fh = priv;
  741. struct em28xx *dev = fh->dev;
  742. switch (a->index) {
  743. case EM28XX_AMUX_VIDEO:
  744. strcpy(a->name, "Television");
  745. break;
  746. case EM28XX_AMUX_LINE_IN:
  747. strcpy(a->name, "Line In");
  748. break;
  749. case EM28XX_AMUX_VIDEO2:
  750. strcpy(a->name, "Television alt");
  751. break;
  752. case EM28XX_AMUX_PHONE:
  753. strcpy(a->name, "Phone");
  754. break;
  755. case EM28XX_AMUX_MIC:
  756. strcpy(a->name, "Mic");
  757. break;
  758. case EM28XX_AMUX_CD:
  759. strcpy(a->name, "CD");
  760. break;
  761. case EM28XX_AMUX_AUX:
  762. strcpy(a->name, "Aux");
  763. break;
  764. case EM28XX_AMUX_PCM_OUT:
  765. strcpy(a->name, "PCM");
  766. break;
  767. default:
  768. return -EINVAL;
  769. }
  770. a->index = dev->ctl_ainput;
  771. a->capability = V4L2_AUDCAP_STEREO;
  772. return 0;
  773. }
  774. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  775. {
  776. struct em28xx_fh *fh = priv;
  777. struct em28xx *dev = fh->dev;
  778. if (a->index >= MAX_EM28XX_INPUT)
  779. return -EINVAL;
  780. if (0 == INPUT(a->index)->type)
  781. return -EINVAL;
  782. mutex_lock(&dev->lock);
  783. dev->ctl_ainput = INPUT(a->index)->amux;
  784. dev->ctl_aoutput = INPUT(a->index)->aout;
  785. if (!dev->ctl_aoutput)
  786. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  787. mutex_unlock(&dev->lock);
  788. return 0;
  789. }
  790. static int vidioc_queryctrl(struct file *file, void *priv,
  791. struct v4l2_queryctrl *qc)
  792. {
  793. struct em28xx_fh *fh = priv;
  794. struct em28xx *dev = fh->dev;
  795. int id = qc->id;
  796. int i;
  797. int rc;
  798. rc = check_dev(dev);
  799. if (rc < 0)
  800. return rc;
  801. memset(qc, 0, sizeof(*qc));
  802. qc->id = id;
  803. if (!dev->board.has_msp34xx) {
  804. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  805. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  806. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  807. return 0;
  808. }
  809. }
  810. }
  811. mutex_lock(&dev->lock);
  812. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  813. mutex_unlock(&dev->lock);
  814. if (qc->type)
  815. return 0;
  816. else
  817. return -EINVAL;
  818. }
  819. static int vidioc_g_ctrl(struct file *file, void *priv,
  820. struct v4l2_control *ctrl)
  821. {
  822. struct em28xx_fh *fh = priv;
  823. struct em28xx *dev = fh->dev;
  824. int rc;
  825. rc = check_dev(dev);
  826. if (rc < 0)
  827. return rc;
  828. rc = 0;
  829. mutex_lock(&dev->lock);
  830. if (dev->board.has_msp34xx)
  831. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  832. else {
  833. rc = em28xx_get_ctrl(dev, ctrl);
  834. if (rc < 0) {
  835. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  836. rc = 0;
  837. }
  838. }
  839. mutex_unlock(&dev->lock);
  840. return rc;
  841. }
  842. static int vidioc_s_ctrl(struct file *file, void *priv,
  843. struct v4l2_control *ctrl)
  844. {
  845. struct em28xx_fh *fh = priv;
  846. struct em28xx *dev = fh->dev;
  847. u8 i;
  848. int rc;
  849. rc = check_dev(dev);
  850. if (rc < 0)
  851. return rc;
  852. mutex_lock(&dev->lock);
  853. if (dev->board.has_msp34xx)
  854. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  855. else {
  856. rc = 1;
  857. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  858. if (ctrl->id == em28xx_qctrl[i].id) {
  859. if (ctrl->value < em28xx_qctrl[i].minimum ||
  860. ctrl->value > em28xx_qctrl[i].maximum) {
  861. rc = -ERANGE;
  862. break;
  863. }
  864. rc = em28xx_set_ctrl(dev, ctrl);
  865. break;
  866. }
  867. }
  868. }
  869. /* Control not found - try to send it to the attached devices */
  870. if (rc == 1) {
  871. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  872. rc = 0;
  873. }
  874. mutex_unlock(&dev->lock);
  875. return rc;
  876. }
  877. static int vidioc_g_tuner(struct file *file, void *priv,
  878. struct v4l2_tuner *t)
  879. {
  880. struct em28xx_fh *fh = priv;
  881. struct em28xx *dev = fh->dev;
  882. int rc;
  883. rc = check_dev(dev);
  884. if (rc < 0)
  885. return rc;
  886. if (0 != t->index)
  887. return -EINVAL;
  888. strcpy(t->name, "Tuner");
  889. mutex_lock(&dev->lock);
  890. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  891. mutex_unlock(&dev->lock);
  892. return 0;
  893. }
  894. static int vidioc_s_tuner(struct file *file, void *priv,
  895. struct v4l2_tuner *t)
  896. {
  897. struct em28xx_fh *fh = priv;
  898. struct em28xx *dev = fh->dev;
  899. int rc;
  900. rc = check_dev(dev);
  901. if (rc < 0)
  902. return rc;
  903. if (0 != t->index)
  904. return -EINVAL;
  905. mutex_lock(&dev->lock);
  906. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  907. mutex_unlock(&dev->lock);
  908. return 0;
  909. }
  910. static int vidioc_g_frequency(struct file *file, void *priv,
  911. struct v4l2_frequency *f)
  912. {
  913. struct em28xx_fh *fh = priv;
  914. struct em28xx *dev = fh->dev;
  915. mutex_lock(&dev->lock);
  916. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  917. f->frequency = dev->ctl_freq;
  918. mutex_unlock(&dev->lock);
  919. return 0;
  920. }
  921. static int vidioc_s_frequency(struct file *file, void *priv,
  922. struct v4l2_frequency *f)
  923. {
  924. struct em28xx_fh *fh = priv;
  925. struct em28xx *dev = fh->dev;
  926. int rc;
  927. rc = check_dev(dev);
  928. if (rc < 0)
  929. return rc;
  930. if (0 != f->tuner)
  931. return -EINVAL;
  932. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  933. return -EINVAL;
  934. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  935. return -EINVAL;
  936. mutex_lock(&dev->lock);
  937. dev->ctl_freq = f->frequency;
  938. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  939. mutex_unlock(&dev->lock);
  940. return 0;
  941. }
  942. #ifdef CONFIG_VIDEO_ADV_DEBUG
  943. static int em28xx_reg_len(int reg)
  944. {
  945. switch (reg) {
  946. case EM28XX_R40_AC97LSB:
  947. case EM28XX_R30_HSCALELOW:
  948. case EM28XX_R32_VSCALELOW:
  949. return 2;
  950. default:
  951. return 1;
  952. }
  953. }
  954. static int vidioc_g_chip_ident(struct file *file, void *priv,
  955. struct v4l2_dbg_chip_ident *chip)
  956. {
  957. struct em28xx_fh *fh = priv;
  958. struct em28xx *dev = fh->dev;
  959. chip->ident = V4L2_IDENT_NONE;
  960. chip->revision = 0;
  961. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
  962. return 0;
  963. }
  964. static int vidioc_g_register(struct file *file, void *priv,
  965. struct v4l2_dbg_register *reg)
  966. {
  967. struct em28xx_fh *fh = priv;
  968. struct em28xx *dev = fh->dev;
  969. int ret;
  970. switch (reg->match.type) {
  971. case V4L2_CHIP_MATCH_AC97:
  972. mutex_lock(&dev->lock);
  973. ret = em28xx_read_ac97(dev, reg->reg);
  974. mutex_unlock(&dev->lock);
  975. if (ret < 0)
  976. return ret;
  977. reg->val = ret;
  978. reg->size = 1;
  979. return 0;
  980. case V4L2_CHIP_MATCH_I2C_DRIVER:
  981. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
  982. return 0;
  983. case V4L2_CHIP_MATCH_I2C_ADDR:
  984. /* Not supported yet */
  985. return -EINVAL;
  986. default:
  987. if (!v4l2_chip_match_host(&reg->match))
  988. return -EINVAL;
  989. }
  990. /* Match host */
  991. reg->size = em28xx_reg_len(reg->reg);
  992. if (reg->size == 1) {
  993. mutex_lock(&dev->lock);
  994. ret = em28xx_read_reg(dev, reg->reg);
  995. mutex_unlock(&dev->lock);
  996. if (ret < 0)
  997. return ret;
  998. reg->val = ret;
  999. } else {
  1000. __le16 val = 0;
  1001. mutex_lock(&dev->lock);
  1002. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1003. reg->reg, (char *)&val, 2);
  1004. mutex_unlock(&dev->lock);
  1005. if (ret < 0)
  1006. return ret;
  1007. reg->val = le16_to_cpu(val);
  1008. }
  1009. return 0;
  1010. }
  1011. static int vidioc_s_register(struct file *file, void *priv,
  1012. struct v4l2_dbg_register *reg)
  1013. {
  1014. struct em28xx_fh *fh = priv;
  1015. struct em28xx *dev = fh->dev;
  1016. __le16 buf;
  1017. int rc;
  1018. switch (reg->match.type) {
  1019. case V4L2_CHIP_MATCH_AC97:
  1020. mutex_lock(&dev->lock);
  1021. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1022. mutex_unlock(&dev->lock);
  1023. return rc;
  1024. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1025. em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
  1026. return 0;
  1027. case V4L2_CHIP_MATCH_I2C_ADDR:
  1028. /* Not supported yet */
  1029. return -EINVAL;
  1030. default:
  1031. if (!v4l2_chip_match_host(&reg->match))
  1032. return -EINVAL;
  1033. }
  1034. /* Match host */
  1035. buf = cpu_to_le16(reg->val);
  1036. mutex_lock(&dev->lock);
  1037. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1038. em28xx_reg_len(reg->reg));
  1039. mutex_unlock(&dev->lock);
  1040. return rc;
  1041. }
  1042. #endif
  1043. static int vidioc_cropcap(struct file *file, void *priv,
  1044. struct v4l2_cropcap *cc)
  1045. {
  1046. struct em28xx_fh *fh = priv;
  1047. struct em28xx *dev = fh->dev;
  1048. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1049. return -EINVAL;
  1050. cc->bounds.left = 0;
  1051. cc->bounds.top = 0;
  1052. cc->bounds.width = dev->width;
  1053. cc->bounds.height = dev->height;
  1054. cc->defrect = cc->bounds;
  1055. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1056. cc->pixelaspect.denominator = 59;
  1057. return 0;
  1058. }
  1059. static int vidioc_streamon(struct file *file, void *priv,
  1060. enum v4l2_buf_type type)
  1061. {
  1062. struct em28xx_fh *fh = priv;
  1063. struct em28xx *dev = fh->dev;
  1064. int rc;
  1065. rc = check_dev(dev);
  1066. if (rc < 0)
  1067. return rc;
  1068. mutex_lock(&dev->lock);
  1069. rc = res_get(fh);
  1070. if (likely(rc >= 0))
  1071. rc = videobuf_streamon(&fh->vb_vidq);
  1072. mutex_unlock(&dev->lock);
  1073. return rc;
  1074. }
  1075. static int vidioc_streamoff(struct file *file, void *priv,
  1076. enum v4l2_buf_type type)
  1077. {
  1078. struct em28xx_fh *fh = priv;
  1079. struct em28xx *dev = fh->dev;
  1080. int rc;
  1081. rc = check_dev(dev);
  1082. if (rc < 0)
  1083. return rc;
  1084. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1085. return -EINVAL;
  1086. if (type != fh->type)
  1087. return -EINVAL;
  1088. mutex_lock(&dev->lock);
  1089. videobuf_streamoff(&fh->vb_vidq);
  1090. res_free(fh);
  1091. mutex_unlock(&dev->lock);
  1092. return 0;
  1093. }
  1094. static int vidioc_querycap(struct file *file, void *priv,
  1095. struct v4l2_capability *cap)
  1096. {
  1097. struct em28xx_fh *fh = priv;
  1098. struct em28xx *dev = fh->dev;
  1099. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1100. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1101. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1102. cap->version = EM28XX_VERSION_CODE;
  1103. cap->capabilities =
  1104. V4L2_CAP_SLICED_VBI_CAPTURE |
  1105. V4L2_CAP_VIDEO_CAPTURE |
  1106. V4L2_CAP_AUDIO |
  1107. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1108. if (dev->tuner_type != TUNER_ABSENT)
  1109. cap->capabilities |= V4L2_CAP_TUNER;
  1110. return 0;
  1111. }
  1112. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1113. struct v4l2_fmtdesc *f)
  1114. {
  1115. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1116. return -EINVAL;
  1117. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1118. f->pixelformat = format[f->index].fourcc;
  1119. return 0;
  1120. }
  1121. /* Sliced VBI ioctls */
  1122. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1123. struct v4l2_format *f)
  1124. {
  1125. struct em28xx_fh *fh = priv;
  1126. struct em28xx *dev = fh->dev;
  1127. int rc;
  1128. rc = check_dev(dev);
  1129. if (rc < 0)
  1130. return rc;
  1131. mutex_lock(&dev->lock);
  1132. f->fmt.sliced.service_set = 0;
  1133. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1134. if (f->fmt.sliced.service_set == 0)
  1135. rc = -EINVAL;
  1136. mutex_unlock(&dev->lock);
  1137. return rc;
  1138. }
  1139. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1140. struct v4l2_format *f)
  1141. {
  1142. struct em28xx_fh *fh = priv;
  1143. struct em28xx *dev = fh->dev;
  1144. int rc;
  1145. rc = check_dev(dev);
  1146. if (rc < 0)
  1147. return rc;
  1148. mutex_lock(&dev->lock);
  1149. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1150. mutex_unlock(&dev->lock);
  1151. if (f->fmt.sliced.service_set == 0)
  1152. return -EINVAL;
  1153. return 0;
  1154. }
  1155. static int vidioc_reqbufs(struct file *file, void *priv,
  1156. struct v4l2_requestbuffers *rb)
  1157. {
  1158. struct em28xx_fh *fh = priv;
  1159. struct em28xx *dev = fh->dev;
  1160. int rc;
  1161. rc = check_dev(dev);
  1162. if (rc < 0)
  1163. return rc;
  1164. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1165. }
  1166. static int vidioc_querybuf(struct file *file, void *priv,
  1167. struct v4l2_buffer *b)
  1168. {
  1169. struct em28xx_fh *fh = priv;
  1170. struct em28xx *dev = fh->dev;
  1171. int rc;
  1172. rc = check_dev(dev);
  1173. if (rc < 0)
  1174. return rc;
  1175. return (videobuf_querybuf(&fh->vb_vidq, b));
  1176. }
  1177. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1178. {
  1179. struct em28xx_fh *fh = priv;
  1180. struct em28xx *dev = fh->dev;
  1181. int rc;
  1182. rc = check_dev(dev);
  1183. if (rc < 0)
  1184. return rc;
  1185. return (videobuf_qbuf(&fh->vb_vidq, b));
  1186. }
  1187. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1188. {
  1189. struct em28xx_fh *fh = priv;
  1190. struct em28xx *dev = fh->dev;
  1191. int rc;
  1192. rc = check_dev(dev);
  1193. if (rc < 0)
  1194. return rc;
  1195. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1196. file->f_flags & O_NONBLOCK));
  1197. }
  1198. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1199. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1200. {
  1201. struct em28xx_fh *fh = priv;
  1202. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1203. }
  1204. #endif
  1205. /* ----------------------------------------------------------- */
  1206. /* RADIO ESPECIFIC IOCTLS */
  1207. /* ----------------------------------------------------------- */
  1208. static int radio_querycap(struct file *file, void *priv,
  1209. struct v4l2_capability *cap)
  1210. {
  1211. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1212. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1213. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1214. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1215. cap->version = EM28XX_VERSION_CODE;
  1216. cap->capabilities = V4L2_CAP_TUNER;
  1217. return 0;
  1218. }
  1219. static int radio_g_tuner(struct file *file, void *priv,
  1220. struct v4l2_tuner *t)
  1221. {
  1222. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1223. if (unlikely(t->index > 0))
  1224. return -EINVAL;
  1225. strcpy(t->name, "Radio");
  1226. t->type = V4L2_TUNER_RADIO;
  1227. mutex_lock(&dev->lock);
  1228. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1229. mutex_unlock(&dev->lock);
  1230. return 0;
  1231. }
  1232. static int radio_enum_input(struct file *file, void *priv,
  1233. struct v4l2_input *i)
  1234. {
  1235. if (i->index != 0)
  1236. return -EINVAL;
  1237. strcpy(i->name, "Radio");
  1238. i->type = V4L2_INPUT_TYPE_TUNER;
  1239. return 0;
  1240. }
  1241. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1242. {
  1243. if (unlikely(a->index))
  1244. return -EINVAL;
  1245. strcpy(a->name, "Radio");
  1246. return 0;
  1247. }
  1248. static int radio_s_tuner(struct file *file, void *priv,
  1249. struct v4l2_tuner *t)
  1250. {
  1251. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1252. if (0 != t->index)
  1253. return -EINVAL;
  1254. mutex_lock(&dev->lock);
  1255. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1256. mutex_unlock(&dev->lock);
  1257. return 0;
  1258. }
  1259. static int radio_s_audio(struct file *file, void *fh,
  1260. struct v4l2_audio *a)
  1261. {
  1262. return 0;
  1263. }
  1264. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1265. {
  1266. return 0;
  1267. }
  1268. static int radio_queryctrl(struct file *file, void *priv,
  1269. struct v4l2_queryctrl *qc)
  1270. {
  1271. int i;
  1272. if (qc->id < V4L2_CID_BASE ||
  1273. qc->id >= V4L2_CID_LASTP1)
  1274. return -EINVAL;
  1275. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1276. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1277. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1278. return 0;
  1279. }
  1280. }
  1281. return -EINVAL;
  1282. }
  1283. /*
  1284. * em28xx_v4l2_open()
  1285. * inits the device and starts isoc transfer
  1286. */
  1287. static int em28xx_v4l2_open(struct file *filp)
  1288. {
  1289. int minor = video_devdata(filp)->minor;
  1290. int errCode = 0, radio;
  1291. struct em28xx *dev;
  1292. enum v4l2_buf_type fh_type;
  1293. struct em28xx_fh *fh;
  1294. dev = em28xx_get_device(minor, &fh_type, &radio);
  1295. if (NULL == dev)
  1296. return -ENODEV;
  1297. mutex_lock(&dev->lock);
  1298. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1299. minor, v4l2_type_names[fh_type], dev->users);
  1300. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1301. if (!fh) {
  1302. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1303. mutex_unlock(&dev->lock);
  1304. return -ENOMEM;
  1305. }
  1306. fh->dev = dev;
  1307. fh->radio = radio;
  1308. fh->type = fh_type;
  1309. filp->private_data = fh;
  1310. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1311. dev->width = norm_maxw(dev);
  1312. dev->height = norm_maxh(dev);
  1313. dev->hscale = 0;
  1314. dev->vscale = 0;
  1315. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1316. em28xx_set_alternate(dev);
  1317. em28xx_resolution_set(dev);
  1318. /* Needed, since GPIO might have disabled power of
  1319. some i2c device
  1320. */
  1321. em28xx_wake_i2c(dev);
  1322. }
  1323. if (fh->radio) {
  1324. em28xx_videodbg("video_open: setting radio device\n");
  1325. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1326. }
  1327. dev->users++;
  1328. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1329. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1330. sizeof(struct em28xx_buffer), fh);
  1331. mutex_unlock(&dev->lock);
  1332. return errCode;
  1333. }
  1334. /*
  1335. * em28xx_realease_resources()
  1336. * unregisters the v4l2,i2c and usb devices
  1337. * called when the device gets disconected or at module unload
  1338. */
  1339. void em28xx_release_analog_resources(struct em28xx *dev)
  1340. {
  1341. /*FIXME: I2C IR should be disconnected */
  1342. if (dev->radio_dev) {
  1343. if (-1 != dev->radio_dev->minor)
  1344. video_unregister_device(dev->radio_dev);
  1345. else
  1346. video_device_release(dev->radio_dev);
  1347. dev->radio_dev = NULL;
  1348. }
  1349. if (dev->vbi_dev) {
  1350. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1351. dev->vbi_dev->num);
  1352. if (-1 != dev->vbi_dev->minor)
  1353. video_unregister_device(dev->vbi_dev);
  1354. else
  1355. video_device_release(dev->vbi_dev);
  1356. dev->vbi_dev = NULL;
  1357. }
  1358. if (dev->vdev) {
  1359. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1360. dev->vdev->num);
  1361. if (-1 != dev->vdev->minor)
  1362. video_unregister_device(dev->vdev);
  1363. else
  1364. video_device_release(dev->vdev);
  1365. dev->vdev = NULL;
  1366. }
  1367. }
  1368. /*
  1369. * em28xx_v4l2_close()
  1370. * stops streaming and deallocates all resources allocated by the v4l2
  1371. * calls and ioctls
  1372. */
  1373. static int em28xx_v4l2_close(struct file *filp)
  1374. {
  1375. struct em28xx_fh *fh = filp->private_data;
  1376. struct em28xx *dev = fh->dev;
  1377. int errCode;
  1378. em28xx_videodbg("users=%d\n", dev->users);
  1379. mutex_lock(&dev->lock);
  1380. if (res_check(fh))
  1381. res_free(fh);
  1382. if (dev->users == 1) {
  1383. videobuf_stop(&fh->vb_vidq);
  1384. videobuf_mmap_free(&fh->vb_vidq);
  1385. /* the device is already disconnect,
  1386. free the remaining resources */
  1387. if (dev->state & DEV_DISCONNECTED) {
  1388. em28xx_release_resources(dev);
  1389. mutex_unlock(&dev->lock);
  1390. kfree(dev);
  1391. return 0;
  1392. }
  1393. /* Save some power by putting tuner to sleep */
  1394. em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
  1395. /* do this before setting alternate! */
  1396. em28xx_uninit_isoc(dev);
  1397. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1398. /* set alternate 0 */
  1399. dev->alt = 0;
  1400. em28xx_videodbg("setting alternate 0\n");
  1401. errCode = usb_set_interface(dev->udev, 0, 0);
  1402. if (errCode < 0) {
  1403. em28xx_errdev("cannot change alternate number to "
  1404. "0 (error=%i)\n", errCode);
  1405. }
  1406. }
  1407. kfree(fh);
  1408. dev->users--;
  1409. wake_up_interruptible_nr(&dev->open, 1);
  1410. mutex_unlock(&dev->lock);
  1411. return 0;
  1412. }
  1413. /*
  1414. * em28xx_v4l2_read()
  1415. * will allocate buffers when called for the first time
  1416. */
  1417. static ssize_t
  1418. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1419. loff_t *pos)
  1420. {
  1421. struct em28xx_fh *fh = filp->private_data;
  1422. struct em28xx *dev = fh->dev;
  1423. int rc;
  1424. rc = check_dev(dev);
  1425. if (rc < 0)
  1426. return rc;
  1427. /* FIXME: read() is not prepared to allow changing the video
  1428. resolution while streaming. Seems a bug at em28xx_set_fmt
  1429. */
  1430. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1431. mutex_lock(&dev->lock);
  1432. rc = res_get(fh);
  1433. mutex_unlock(&dev->lock);
  1434. if (unlikely(rc < 0))
  1435. return rc;
  1436. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1437. filp->f_flags & O_NONBLOCK);
  1438. }
  1439. return 0;
  1440. }
  1441. /*
  1442. * em28xx_v4l2_poll()
  1443. * will allocate buffers when called for the first time
  1444. */
  1445. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1446. {
  1447. struct em28xx_fh *fh = filp->private_data;
  1448. struct em28xx *dev = fh->dev;
  1449. int rc;
  1450. rc = check_dev(dev);
  1451. if (rc < 0)
  1452. return rc;
  1453. mutex_lock(&dev->lock);
  1454. rc = res_get(fh);
  1455. mutex_unlock(&dev->lock);
  1456. if (unlikely(rc < 0))
  1457. return POLLERR;
  1458. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1459. return POLLERR;
  1460. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1461. }
  1462. /*
  1463. * em28xx_v4l2_mmap()
  1464. */
  1465. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1466. {
  1467. struct em28xx_fh *fh = filp->private_data;
  1468. struct em28xx *dev = fh->dev;
  1469. int rc;
  1470. rc = check_dev(dev);
  1471. if (rc < 0)
  1472. return rc;
  1473. mutex_lock(&dev->lock);
  1474. rc = res_get(fh);
  1475. mutex_unlock(&dev->lock);
  1476. if (unlikely(rc < 0))
  1477. return rc;
  1478. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1479. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1480. (unsigned long)vma->vm_start,
  1481. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1482. rc);
  1483. return rc;
  1484. }
  1485. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1486. .owner = THIS_MODULE,
  1487. .open = em28xx_v4l2_open,
  1488. .release = em28xx_v4l2_close,
  1489. .read = em28xx_v4l2_read,
  1490. .poll = em28xx_v4l2_poll,
  1491. .mmap = em28xx_v4l2_mmap,
  1492. .ioctl = video_ioctl2,
  1493. };
  1494. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1495. .vidioc_querycap = vidioc_querycap,
  1496. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1497. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1498. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1499. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1500. .vidioc_g_audio = vidioc_g_audio,
  1501. .vidioc_s_audio = vidioc_s_audio,
  1502. .vidioc_cropcap = vidioc_cropcap,
  1503. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1504. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1505. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1506. .vidioc_reqbufs = vidioc_reqbufs,
  1507. .vidioc_querybuf = vidioc_querybuf,
  1508. .vidioc_qbuf = vidioc_qbuf,
  1509. .vidioc_dqbuf = vidioc_dqbuf,
  1510. .vidioc_s_std = vidioc_s_std,
  1511. .vidioc_enum_input = vidioc_enum_input,
  1512. .vidioc_g_input = vidioc_g_input,
  1513. .vidioc_s_input = vidioc_s_input,
  1514. .vidioc_queryctrl = vidioc_queryctrl,
  1515. .vidioc_g_ctrl = vidioc_g_ctrl,
  1516. .vidioc_s_ctrl = vidioc_s_ctrl,
  1517. .vidioc_streamon = vidioc_streamon,
  1518. .vidioc_streamoff = vidioc_streamoff,
  1519. .vidioc_g_tuner = vidioc_g_tuner,
  1520. .vidioc_s_tuner = vidioc_s_tuner,
  1521. .vidioc_g_frequency = vidioc_g_frequency,
  1522. .vidioc_s_frequency = vidioc_s_frequency,
  1523. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1524. .vidioc_g_register = vidioc_g_register,
  1525. .vidioc_s_register = vidioc_s_register,
  1526. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1527. #endif
  1528. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1529. .vidiocgmbuf = vidiocgmbuf,
  1530. #endif
  1531. };
  1532. static const struct video_device em28xx_video_template = {
  1533. .fops = &em28xx_v4l_fops,
  1534. .release = video_device_release,
  1535. .ioctl_ops = &video_ioctl_ops,
  1536. .minor = -1,
  1537. .tvnorms = V4L2_STD_ALL,
  1538. .current_norm = V4L2_STD_PAL,
  1539. };
  1540. static const struct v4l2_file_operations radio_fops = {
  1541. .owner = THIS_MODULE,
  1542. .open = em28xx_v4l2_open,
  1543. .release = em28xx_v4l2_close,
  1544. .ioctl = video_ioctl2,
  1545. };
  1546. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1547. .vidioc_querycap = radio_querycap,
  1548. .vidioc_g_tuner = radio_g_tuner,
  1549. .vidioc_enum_input = radio_enum_input,
  1550. .vidioc_g_audio = radio_g_audio,
  1551. .vidioc_s_tuner = radio_s_tuner,
  1552. .vidioc_s_audio = radio_s_audio,
  1553. .vidioc_s_input = radio_s_input,
  1554. .vidioc_queryctrl = radio_queryctrl,
  1555. .vidioc_g_ctrl = vidioc_g_ctrl,
  1556. .vidioc_s_ctrl = vidioc_s_ctrl,
  1557. .vidioc_g_frequency = vidioc_g_frequency,
  1558. .vidioc_s_frequency = vidioc_s_frequency,
  1559. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1560. .vidioc_g_register = vidioc_g_register,
  1561. .vidioc_s_register = vidioc_s_register,
  1562. #endif
  1563. };
  1564. static struct video_device em28xx_radio_template = {
  1565. .name = "em28xx-radio",
  1566. .fops = &radio_fops,
  1567. .ioctl_ops = &radio_ioctl_ops,
  1568. .minor = -1,
  1569. };
  1570. /******************************** usb interface ******************************/
  1571. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1572. const struct video_device *template,
  1573. const char *type_name)
  1574. {
  1575. struct video_device *vfd;
  1576. vfd = video_device_alloc();
  1577. if (NULL == vfd)
  1578. return NULL;
  1579. *vfd = *template;
  1580. vfd->minor = -1;
  1581. vfd->parent = &dev->udev->dev;
  1582. vfd->release = video_device_release;
  1583. vfd->debug = video_debug;
  1584. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1585. dev->name, type_name);
  1586. return vfd;
  1587. }
  1588. int em28xx_register_analog_devices(struct em28xx *dev)
  1589. {
  1590. u8 val;
  1591. int ret;
  1592. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1593. dev->name,
  1594. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1595. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1596. /* set default norm */
  1597. dev->norm = em28xx_video_template.current_norm;
  1598. dev->width = norm_maxw(dev);
  1599. dev->height = norm_maxh(dev);
  1600. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1601. dev->hscale = 0;
  1602. dev->vscale = 0;
  1603. dev->ctl_input = 0;
  1604. /* Analog specific initialization */
  1605. dev->format = &format[0];
  1606. video_mux(dev, dev->ctl_input);
  1607. /* Audio defaults */
  1608. dev->mute = 1;
  1609. dev->volume = 0x1f;
  1610. /* enable vbi capturing */
  1611. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1612. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1613. em28xx_write_reg(dev, EM28XX_R0F_XCLK, (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1614. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1615. em28xx_set_outfmt(dev);
  1616. em28xx_colorlevels_set_default(dev);
  1617. em28xx_compression_disable(dev);
  1618. /* allocate and fill video video_device struct */
  1619. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1620. if (!dev->vdev) {
  1621. em28xx_errdev("cannot allocate video_device.\n");
  1622. return -ENODEV;
  1623. }
  1624. /* register v4l2 video video_device */
  1625. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1626. video_nr[dev->devno]);
  1627. if (ret) {
  1628. em28xx_errdev("unable to register video device (error=%i).\n",
  1629. ret);
  1630. return ret;
  1631. }
  1632. /* Allocate and fill vbi video_device struct */
  1633. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1634. /* register v4l2 vbi video_device */
  1635. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1636. vbi_nr[dev->devno]);
  1637. if (ret < 0) {
  1638. em28xx_errdev("unable to register vbi device\n");
  1639. return ret;
  1640. }
  1641. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1642. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
  1643. if (!dev->radio_dev) {
  1644. em28xx_errdev("cannot allocate video_device.\n");
  1645. return -ENODEV;
  1646. }
  1647. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1648. radio_nr[dev->devno]);
  1649. if (ret < 0) {
  1650. em28xx_errdev("can't register radio device\n");
  1651. return ret;
  1652. }
  1653. em28xx_info("Registered radio device as /dev/radio%d\n",
  1654. dev->radio_dev->num);
  1655. }
  1656. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1657. dev->vdev->num, dev->vbi_dev->num);
  1658. return 0;
  1659. }