em28xx-video.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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. mutex_unlock(&dev->lock);
  835. return rc;
  836. }
  837. static int vidioc_s_ctrl(struct file *file, void *priv,
  838. struct v4l2_control *ctrl)
  839. {
  840. struct em28xx_fh *fh = priv;
  841. struct em28xx *dev = fh->dev;
  842. u8 i;
  843. int rc;
  844. rc = check_dev(dev);
  845. if (rc < 0)
  846. return rc;
  847. mutex_lock(&dev->lock);
  848. if (dev->board.has_msp34xx)
  849. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  850. else {
  851. rc = 1;
  852. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  853. if (ctrl->id == em28xx_qctrl[i].id) {
  854. if (ctrl->value < em28xx_qctrl[i].minimum ||
  855. ctrl->value > em28xx_qctrl[i].maximum) {
  856. rc = -ERANGE;
  857. break;
  858. }
  859. rc = em28xx_set_ctrl(dev, ctrl);
  860. break;
  861. }
  862. }
  863. }
  864. /* Control not found - try to send it to the attached devices */
  865. if (rc == 1) {
  866. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  867. rc = 0;
  868. }
  869. mutex_unlock(&dev->lock);
  870. return rc;
  871. }
  872. static int vidioc_g_tuner(struct file *file, void *priv,
  873. struct v4l2_tuner *t)
  874. {
  875. struct em28xx_fh *fh = priv;
  876. struct em28xx *dev = fh->dev;
  877. int rc;
  878. rc = check_dev(dev);
  879. if (rc < 0)
  880. return rc;
  881. if (0 != t->index)
  882. return -EINVAL;
  883. strcpy(t->name, "Tuner");
  884. mutex_lock(&dev->lock);
  885. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  886. mutex_unlock(&dev->lock);
  887. return 0;
  888. }
  889. static int vidioc_s_tuner(struct file *file, void *priv,
  890. struct v4l2_tuner *t)
  891. {
  892. struct em28xx_fh *fh = priv;
  893. struct em28xx *dev = fh->dev;
  894. int rc;
  895. rc = check_dev(dev);
  896. if (rc < 0)
  897. return rc;
  898. if (0 != t->index)
  899. return -EINVAL;
  900. mutex_lock(&dev->lock);
  901. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  902. mutex_unlock(&dev->lock);
  903. return 0;
  904. }
  905. static int vidioc_g_frequency(struct file *file, void *priv,
  906. struct v4l2_frequency *f)
  907. {
  908. struct em28xx_fh *fh = priv;
  909. struct em28xx *dev = fh->dev;
  910. mutex_lock(&dev->lock);
  911. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  912. f->frequency = dev->ctl_freq;
  913. mutex_unlock(&dev->lock);
  914. return 0;
  915. }
  916. static int vidioc_s_frequency(struct file *file, void *priv,
  917. struct v4l2_frequency *f)
  918. {
  919. struct em28xx_fh *fh = priv;
  920. struct em28xx *dev = fh->dev;
  921. int rc;
  922. rc = check_dev(dev);
  923. if (rc < 0)
  924. return rc;
  925. if (0 != f->tuner)
  926. return -EINVAL;
  927. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  928. return -EINVAL;
  929. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  930. return -EINVAL;
  931. mutex_lock(&dev->lock);
  932. dev->ctl_freq = f->frequency;
  933. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  934. mutex_unlock(&dev->lock);
  935. return 0;
  936. }
  937. #ifdef CONFIG_VIDEO_ADV_DEBUG
  938. static int em28xx_reg_len(int reg)
  939. {
  940. switch (reg) {
  941. case EM28XX_R40_AC97LSB:
  942. case EM28XX_R30_HSCALELOW:
  943. case EM28XX_R32_VSCALELOW:
  944. return 2;
  945. default:
  946. return 1;
  947. }
  948. }
  949. static int vidioc_g_chip_ident(struct file *file, void *priv,
  950. struct v4l2_dbg_chip_ident *chip)
  951. {
  952. struct em28xx_fh *fh = priv;
  953. struct em28xx *dev = fh->dev;
  954. chip->ident = V4L2_IDENT_NONE;
  955. chip->revision = 0;
  956. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
  957. return 0;
  958. }
  959. static int vidioc_g_register(struct file *file, void *priv,
  960. struct v4l2_dbg_register *reg)
  961. {
  962. struct em28xx_fh *fh = priv;
  963. struct em28xx *dev = fh->dev;
  964. int ret;
  965. switch (reg->match.type) {
  966. case V4L2_CHIP_MATCH_AC97:
  967. mutex_lock(&dev->lock);
  968. ret = em28xx_read_ac97(dev, reg->reg);
  969. mutex_unlock(&dev->lock);
  970. if (ret < 0)
  971. return ret;
  972. reg->val = ret;
  973. reg->size = 1;
  974. return 0;
  975. case V4L2_CHIP_MATCH_I2C_DRIVER:
  976. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
  977. return 0;
  978. case V4L2_CHIP_MATCH_I2C_ADDR:
  979. /* Not supported yet */
  980. return -EINVAL;
  981. default:
  982. if (!v4l2_chip_match_host(&reg->match))
  983. return -EINVAL;
  984. }
  985. /* Match host */
  986. reg->size = em28xx_reg_len(reg->reg);
  987. if (reg->size == 1) {
  988. mutex_lock(&dev->lock);
  989. ret = em28xx_read_reg(dev, reg->reg);
  990. mutex_unlock(&dev->lock);
  991. if (ret < 0)
  992. return ret;
  993. reg->val = ret;
  994. } else {
  995. __le16 val = 0;
  996. mutex_lock(&dev->lock);
  997. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  998. reg->reg, (char *)&val, 2);
  999. mutex_unlock(&dev->lock);
  1000. if (ret < 0)
  1001. return ret;
  1002. reg->val = le16_to_cpu(val);
  1003. }
  1004. return 0;
  1005. }
  1006. static int vidioc_s_register(struct file *file, void *priv,
  1007. struct v4l2_dbg_register *reg)
  1008. {
  1009. struct em28xx_fh *fh = priv;
  1010. struct em28xx *dev = fh->dev;
  1011. __le16 buf;
  1012. int rc;
  1013. switch (reg->match.type) {
  1014. case V4L2_CHIP_MATCH_AC97:
  1015. mutex_lock(&dev->lock);
  1016. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1017. mutex_unlock(&dev->lock);
  1018. return rc;
  1019. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1020. em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
  1021. return 0;
  1022. case V4L2_CHIP_MATCH_I2C_ADDR:
  1023. /* Not supported yet */
  1024. return -EINVAL;
  1025. default:
  1026. if (!v4l2_chip_match_host(&reg->match))
  1027. return -EINVAL;
  1028. }
  1029. /* Match host */
  1030. buf = cpu_to_le16(reg->val);
  1031. mutex_lock(&dev->lock);
  1032. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1033. em28xx_reg_len(reg->reg));
  1034. mutex_unlock(&dev->lock);
  1035. return rc;
  1036. }
  1037. #endif
  1038. static int vidioc_cropcap(struct file *file, void *priv,
  1039. struct v4l2_cropcap *cc)
  1040. {
  1041. struct em28xx_fh *fh = priv;
  1042. struct em28xx *dev = fh->dev;
  1043. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1044. return -EINVAL;
  1045. cc->bounds.left = 0;
  1046. cc->bounds.top = 0;
  1047. cc->bounds.width = dev->width;
  1048. cc->bounds.height = dev->height;
  1049. cc->defrect = cc->bounds;
  1050. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1051. cc->pixelaspect.denominator = 59;
  1052. return 0;
  1053. }
  1054. static int vidioc_streamon(struct file *file, void *priv,
  1055. enum v4l2_buf_type type)
  1056. {
  1057. struct em28xx_fh *fh = priv;
  1058. struct em28xx *dev = fh->dev;
  1059. int rc;
  1060. rc = check_dev(dev);
  1061. if (rc < 0)
  1062. return rc;
  1063. mutex_lock(&dev->lock);
  1064. rc = res_get(fh);
  1065. if (likely(rc >= 0))
  1066. rc = videobuf_streamon(&fh->vb_vidq);
  1067. mutex_unlock(&dev->lock);
  1068. return rc;
  1069. }
  1070. static int vidioc_streamoff(struct file *file, void *priv,
  1071. enum v4l2_buf_type type)
  1072. {
  1073. struct em28xx_fh *fh = priv;
  1074. struct em28xx *dev = fh->dev;
  1075. int rc;
  1076. rc = check_dev(dev);
  1077. if (rc < 0)
  1078. return rc;
  1079. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1080. return -EINVAL;
  1081. if (type != fh->type)
  1082. return -EINVAL;
  1083. mutex_lock(&dev->lock);
  1084. videobuf_streamoff(&fh->vb_vidq);
  1085. res_free(fh);
  1086. mutex_unlock(&dev->lock);
  1087. return 0;
  1088. }
  1089. static int vidioc_querycap(struct file *file, void *priv,
  1090. struct v4l2_capability *cap)
  1091. {
  1092. struct em28xx_fh *fh = priv;
  1093. struct em28xx *dev = fh->dev;
  1094. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1095. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1096. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1097. cap->version = EM28XX_VERSION_CODE;
  1098. cap->capabilities =
  1099. V4L2_CAP_SLICED_VBI_CAPTURE |
  1100. V4L2_CAP_VIDEO_CAPTURE |
  1101. V4L2_CAP_AUDIO |
  1102. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1103. if (dev->tuner_type != TUNER_ABSENT)
  1104. cap->capabilities |= V4L2_CAP_TUNER;
  1105. return 0;
  1106. }
  1107. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1108. struct v4l2_fmtdesc *f)
  1109. {
  1110. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1111. return -EINVAL;
  1112. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1113. f->pixelformat = format[f->index].fourcc;
  1114. return 0;
  1115. }
  1116. /* Sliced VBI ioctls */
  1117. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1118. struct v4l2_format *f)
  1119. {
  1120. struct em28xx_fh *fh = priv;
  1121. struct em28xx *dev = fh->dev;
  1122. int rc;
  1123. rc = check_dev(dev);
  1124. if (rc < 0)
  1125. return rc;
  1126. mutex_lock(&dev->lock);
  1127. f->fmt.sliced.service_set = 0;
  1128. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1129. if (f->fmt.sliced.service_set == 0)
  1130. rc = -EINVAL;
  1131. mutex_unlock(&dev->lock);
  1132. return rc;
  1133. }
  1134. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1135. struct v4l2_format *f)
  1136. {
  1137. struct em28xx_fh *fh = priv;
  1138. struct em28xx *dev = fh->dev;
  1139. int rc;
  1140. rc = check_dev(dev);
  1141. if (rc < 0)
  1142. return rc;
  1143. mutex_lock(&dev->lock);
  1144. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1145. mutex_unlock(&dev->lock);
  1146. if (f->fmt.sliced.service_set == 0)
  1147. return -EINVAL;
  1148. return 0;
  1149. }
  1150. static int vidioc_reqbufs(struct file *file, void *priv,
  1151. struct v4l2_requestbuffers *rb)
  1152. {
  1153. struct em28xx_fh *fh = priv;
  1154. struct em28xx *dev = fh->dev;
  1155. int rc;
  1156. rc = check_dev(dev);
  1157. if (rc < 0)
  1158. return rc;
  1159. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1160. }
  1161. static int vidioc_querybuf(struct file *file, void *priv,
  1162. struct v4l2_buffer *b)
  1163. {
  1164. struct em28xx_fh *fh = priv;
  1165. struct em28xx *dev = fh->dev;
  1166. int rc;
  1167. rc = check_dev(dev);
  1168. if (rc < 0)
  1169. return rc;
  1170. return (videobuf_querybuf(&fh->vb_vidq, b));
  1171. }
  1172. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1173. {
  1174. struct em28xx_fh *fh = priv;
  1175. struct em28xx *dev = fh->dev;
  1176. int rc;
  1177. rc = check_dev(dev);
  1178. if (rc < 0)
  1179. return rc;
  1180. return (videobuf_qbuf(&fh->vb_vidq, b));
  1181. }
  1182. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1183. {
  1184. struct em28xx_fh *fh = priv;
  1185. struct em28xx *dev = fh->dev;
  1186. int rc;
  1187. rc = check_dev(dev);
  1188. if (rc < 0)
  1189. return rc;
  1190. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1191. file->f_flags & O_NONBLOCK));
  1192. }
  1193. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1194. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1195. {
  1196. struct em28xx_fh *fh = priv;
  1197. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1198. }
  1199. #endif
  1200. /* ----------------------------------------------------------- */
  1201. /* RADIO ESPECIFIC IOCTLS */
  1202. /* ----------------------------------------------------------- */
  1203. static int radio_querycap(struct file *file, void *priv,
  1204. struct v4l2_capability *cap)
  1205. {
  1206. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1207. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1208. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1209. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1210. cap->version = EM28XX_VERSION_CODE;
  1211. cap->capabilities = V4L2_CAP_TUNER;
  1212. return 0;
  1213. }
  1214. static int radio_g_tuner(struct file *file, void *priv,
  1215. struct v4l2_tuner *t)
  1216. {
  1217. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1218. if (unlikely(t->index > 0))
  1219. return -EINVAL;
  1220. strcpy(t->name, "Radio");
  1221. t->type = V4L2_TUNER_RADIO;
  1222. mutex_lock(&dev->lock);
  1223. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1224. mutex_unlock(&dev->lock);
  1225. return 0;
  1226. }
  1227. static int radio_enum_input(struct file *file, void *priv,
  1228. struct v4l2_input *i)
  1229. {
  1230. if (i->index != 0)
  1231. return -EINVAL;
  1232. strcpy(i->name, "Radio");
  1233. i->type = V4L2_INPUT_TYPE_TUNER;
  1234. return 0;
  1235. }
  1236. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1237. {
  1238. if (unlikely(a->index))
  1239. return -EINVAL;
  1240. strcpy(a->name, "Radio");
  1241. return 0;
  1242. }
  1243. static int radio_s_tuner(struct file *file, void *priv,
  1244. struct v4l2_tuner *t)
  1245. {
  1246. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1247. if (0 != t->index)
  1248. return -EINVAL;
  1249. mutex_lock(&dev->lock);
  1250. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1251. mutex_unlock(&dev->lock);
  1252. return 0;
  1253. }
  1254. static int radio_s_audio(struct file *file, void *fh,
  1255. struct v4l2_audio *a)
  1256. {
  1257. return 0;
  1258. }
  1259. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1260. {
  1261. return 0;
  1262. }
  1263. static int radio_queryctrl(struct file *file, void *priv,
  1264. struct v4l2_queryctrl *qc)
  1265. {
  1266. int i;
  1267. if (qc->id < V4L2_CID_BASE ||
  1268. qc->id >= V4L2_CID_LASTP1)
  1269. return -EINVAL;
  1270. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1271. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1272. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1273. return 0;
  1274. }
  1275. }
  1276. return -EINVAL;
  1277. }
  1278. /*
  1279. * em28xx_v4l2_open()
  1280. * inits the device and starts isoc transfer
  1281. */
  1282. static int em28xx_v4l2_open(struct file *filp)
  1283. {
  1284. int minor = video_devdata(filp)->minor;
  1285. int errCode = 0, radio;
  1286. struct em28xx *dev;
  1287. enum v4l2_buf_type fh_type;
  1288. struct em28xx_fh *fh;
  1289. dev = em28xx_get_device(minor, &fh_type, &radio);
  1290. if (NULL == dev)
  1291. return -ENODEV;
  1292. mutex_lock(&dev->lock);
  1293. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1294. minor, v4l2_type_names[fh_type], dev->users);
  1295. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1296. if (!fh) {
  1297. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1298. mutex_unlock(&dev->lock);
  1299. return -ENOMEM;
  1300. }
  1301. fh->dev = dev;
  1302. fh->radio = radio;
  1303. fh->type = fh_type;
  1304. filp->private_data = fh;
  1305. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1306. dev->width = norm_maxw(dev);
  1307. dev->height = norm_maxh(dev);
  1308. dev->hscale = 0;
  1309. dev->vscale = 0;
  1310. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1311. em28xx_set_alternate(dev);
  1312. em28xx_resolution_set(dev);
  1313. /* Needed, since GPIO might have disabled power of
  1314. some i2c device
  1315. */
  1316. em28xx_wake_i2c(dev);
  1317. }
  1318. if (fh->radio) {
  1319. em28xx_videodbg("video_open: setting radio device\n");
  1320. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1321. }
  1322. dev->users++;
  1323. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1324. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1325. sizeof(struct em28xx_buffer), fh);
  1326. mutex_unlock(&dev->lock);
  1327. return errCode;
  1328. }
  1329. /*
  1330. * em28xx_realease_resources()
  1331. * unregisters the v4l2,i2c and usb devices
  1332. * called when the device gets disconected or at module unload
  1333. */
  1334. void em28xx_release_analog_resources(struct em28xx *dev)
  1335. {
  1336. /*FIXME: I2C IR should be disconnected */
  1337. if (dev->radio_dev) {
  1338. if (-1 != dev->radio_dev->minor)
  1339. video_unregister_device(dev->radio_dev);
  1340. else
  1341. video_device_release(dev->radio_dev);
  1342. dev->radio_dev = NULL;
  1343. }
  1344. if (dev->vbi_dev) {
  1345. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1346. dev->vbi_dev->num);
  1347. if (-1 != dev->vbi_dev->minor)
  1348. video_unregister_device(dev->vbi_dev);
  1349. else
  1350. video_device_release(dev->vbi_dev);
  1351. dev->vbi_dev = NULL;
  1352. }
  1353. if (dev->vdev) {
  1354. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1355. dev->vdev->num);
  1356. if (-1 != dev->vdev->minor)
  1357. video_unregister_device(dev->vdev);
  1358. else
  1359. video_device_release(dev->vdev);
  1360. dev->vdev = NULL;
  1361. }
  1362. }
  1363. /*
  1364. * em28xx_v4l2_close()
  1365. * stops streaming and deallocates all resources allocated by the v4l2
  1366. * calls and ioctls
  1367. */
  1368. static int em28xx_v4l2_close(struct file *filp)
  1369. {
  1370. struct em28xx_fh *fh = filp->private_data;
  1371. struct em28xx *dev = fh->dev;
  1372. int errCode;
  1373. em28xx_videodbg("users=%d\n", dev->users);
  1374. mutex_lock(&dev->lock);
  1375. if (res_check(fh))
  1376. res_free(fh);
  1377. if (dev->users == 1) {
  1378. videobuf_stop(&fh->vb_vidq);
  1379. videobuf_mmap_free(&fh->vb_vidq);
  1380. /* the device is already disconnect,
  1381. free the remaining resources */
  1382. if (dev->state & DEV_DISCONNECTED) {
  1383. em28xx_release_resources(dev);
  1384. mutex_unlock(&dev->lock);
  1385. kfree(dev);
  1386. return 0;
  1387. }
  1388. /* Save some power by putting tuner to sleep */
  1389. em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
  1390. /* do this before setting alternate! */
  1391. em28xx_uninit_isoc(dev);
  1392. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1393. /* set alternate 0 */
  1394. dev->alt = 0;
  1395. em28xx_videodbg("setting alternate 0\n");
  1396. errCode = usb_set_interface(dev->udev, 0, 0);
  1397. if (errCode < 0) {
  1398. em28xx_errdev("cannot change alternate number to "
  1399. "0 (error=%i)\n", errCode);
  1400. }
  1401. }
  1402. kfree(fh);
  1403. dev->users--;
  1404. wake_up_interruptible_nr(&dev->open, 1);
  1405. mutex_unlock(&dev->lock);
  1406. return 0;
  1407. }
  1408. /*
  1409. * em28xx_v4l2_read()
  1410. * will allocate buffers when called for the first time
  1411. */
  1412. static ssize_t
  1413. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1414. loff_t *pos)
  1415. {
  1416. struct em28xx_fh *fh = filp->private_data;
  1417. struct em28xx *dev = fh->dev;
  1418. int rc;
  1419. rc = check_dev(dev);
  1420. if (rc < 0)
  1421. return rc;
  1422. /* FIXME: read() is not prepared to allow changing the video
  1423. resolution while streaming. Seems a bug at em28xx_set_fmt
  1424. */
  1425. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1426. mutex_lock(&dev->lock);
  1427. rc = res_get(fh);
  1428. mutex_unlock(&dev->lock);
  1429. if (unlikely(rc < 0))
  1430. return rc;
  1431. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1432. filp->f_flags & O_NONBLOCK);
  1433. }
  1434. return 0;
  1435. }
  1436. /*
  1437. * em28xx_v4l2_poll()
  1438. * will allocate buffers when called for the first time
  1439. */
  1440. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1441. {
  1442. struct em28xx_fh *fh = filp->private_data;
  1443. struct em28xx *dev = fh->dev;
  1444. int rc;
  1445. rc = check_dev(dev);
  1446. if (rc < 0)
  1447. return rc;
  1448. mutex_lock(&dev->lock);
  1449. rc = res_get(fh);
  1450. mutex_unlock(&dev->lock);
  1451. if (unlikely(rc < 0))
  1452. return POLLERR;
  1453. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1454. return POLLERR;
  1455. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1456. }
  1457. /*
  1458. * em28xx_v4l2_mmap()
  1459. */
  1460. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1461. {
  1462. struct em28xx_fh *fh = filp->private_data;
  1463. struct em28xx *dev = fh->dev;
  1464. int rc;
  1465. rc = check_dev(dev);
  1466. if (rc < 0)
  1467. return rc;
  1468. mutex_lock(&dev->lock);
  1469. rc = res_get(fh);
  1470. mutex_unlock(&dev->lock);
  1471. if (unlikely(rc < 0))
  1472. return rc;
  1473. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1474. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1475. (unsigned long)vma->vm_start,
  1476. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1477. rc);
  1478. return rc;
  1479. }
  1480. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1481. .owner = THIS_MODULE,
  1482. .open = em28xx_v4l2_open,
  1483. .release = em28xx_v4l2_close,
  1484. .read = em28xx_v4l2_read,
  1485. .poll = em28xx_v4l2_poll,
  1486. .mmap = em28xx_v4l2_mmap,
  1487. .ioctl = video_ioctl2,
  1488. };
  1489. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1490. .vidioc_querycap = vidioc_querycap,
  1491. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1492. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1493. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1494. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1495. .vidioc_g_audio = vidioc_g_audio,
  1496. .vidioc_s_audio = vidioc_s_audio,
  1497. .vidioc_cropcap = vidioc_cropcap,
  1498. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1499. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1500. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1501. .vidioc_reqbufs = vidioc_reqbufs,
  1502. .vidioc_querybuf = vidioc_querybuf,
  1503. .vidioc_qbuf = vidioc_qbuf,
  1504. .vidioc_dqbuf = vidioc_dqbuf,
  1505. .vidioc_s_std = vidioc_s_std,
  1506. .vidioc_enum_input = vidioc_enum_input,
  1507. .vidioc_g_input = vidioc_g_input,
  1508. .vidioc_s_input = vidioc_s_input,
  1509. .vidioc_queryctrl = vidioc_queryctrl,
  1510. .vidioc_g_ctrl = vidioc_g_ctrl,
  1511. .vidioc_s_ctrl = vidioc_s_ctrl,
  1512. .vidioc_streamon = vidioc_streamon,
  1513. .vidioc_streamoff = vidioc_streamoff,
  1514. .vidioc_g_tuner = vidioc_g_tuner,
  1515. .vidioc_s_tuner = vidioc_s_tuner,
  1516. .vidioc_g_frequency = vidioc_g_frequency,
  1517. .vidioc_s_frequency = vidioc_s_frequency,
  1518. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1519. .vidioc_g_register = vidioc_g_register,
  1520. .vidioc_s_register = vidioc_s_register,
  1521. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1522. #endif
  1523. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1524. .vidiocgmbuf = vidiocgmbuf,
  1525. #endif
  1526. };
  1527. static const struct video_device em28xx_video_template = {
  1528. .fops = &em28xx_v4l_fops,
  1529. .release = video_device_release,
  1530. .ioctl_ops = &video_ioctl_ops,
  1531. .minor = -1,
  1532. .tvnorms = V4L2_STD_ALL,
  1533. .current_norm = V4L2_STD_PAL,
  1534. };
  1535. static const struct v4l2_file_operations radio_fops = {
  1536. .owner = THIS_MODULE,
  1537. .open = em28xx_v4l2_open,
  1538. .release = em28xx_v4l2_close,
  1539. .ioctl = video_ioctl2,
  1540. };
  1541. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1542. .vidioc_querycap = radio_querycap,
  1543. .vidioc_g_tuner = radio_g_tuner,
  1544. .vidioc_enum_input = radio_enum_input,
  1545. .vidioc_g_audio = radio_g_audio,
  1546. .vidioc_s_tuner = radio_s_tuner,
  1547. .vidioc_s_audio = radio_s_audio,
  1548. .vidioc_s_input = radio_s_input,
  1549. .vidioc_queryctrl = radio_queryctrl,
  1550. .vidioc_g_ctrl = vidioc_g_ctrl,
  1551. .vidioc_s_ctrl = vidioc_s_ctrl,
  1552. .vidioc_g_frequency = vidioc_g_frequency,
  1553. .vidioc_s_frequency = vidioc_s_frequency,
  1554. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1555. .vidioc_g_register = vidioc_g_register,
  1556. .vidioc_s_register = vidioc_s_register,
  1557. #endif
  1558. };
  1559. static struct video_device em28xx_radio_template = {
  1560. .name = "em28xx-radio",
  1561. .fops = &radio_fops,
  1562. .ioctl_ops = &radio_ioctl_ops,
  1563. .minor = -1,
  1564. };
  1565. /******************************** usb interface ******************************/
  1566. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1567. const struct video_device *template,
  1568. const char *type_name)
  1569. {
  1570. struct video_device *vfd;
  1571. vfd = video_device_alloc();
  1572. if (NULL == vfd)
  1573. return NULL;
  1574. *vfd = *template;
  1575. vfd->minor = -1;
  1576. vfd->parent = &dev->udev->dev;
  1577. vfd->release = video_device_release;
  1578. vfd->debug = video_debug;
  1579. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1580. dev->name, type_name);
  1581. return vfd;
  1582. }
  1583. int em28xx_register_analog_devices(struct em28xx *dev)
  1584. {
  1585. int ret;
  1586. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1587. dev->name,
  1588. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1589. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1590. /* set default norm */
  1591. dev->norm = em28xx_video_template.current_norm;
  1592. dev->width = norm_maxw(dev);
  1593. dev->height = norm_maxh(dev);
  1594. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1595. dev->hscale = 0;
  1596. dev->vscale = 0;
  1597. dev->ctl_input = 0;
  1598. /* Analog specific initialization */
  1599. dev->format = &format[0];
  1600. video_mux(dev, dev->ctl_input);
  1601. /* Audio defaults */
  1602. dev->mute = 1;
  1603. dev->volume = 0x1f;
  1604. /* enable vbi capturing */
  1605. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1606. /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
  1607. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1608. em28xx_set_outfmt(dev);
  1609. em28xx_colorlevels_set_default(dev);
  1610. em28xx_compression_disable(dev);
  1611. /* allocate and fill video video_device struct */
  1612. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1613. if (!dev->vdev) {
  1614. em28xx_errdev("cannot allocate video_device.\n");
  1615. return -ENODEV;
  1616. }
  1617. /* register v4l2 video video_device */
  1618. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1619. video_nr[dev->devno]);
  1620. if (ret) {
  1621. em28xx_errdev("unable to register video device (error=%i).\n",
  1622. ret);
  1623. return ret;
  1624. }
  1625. /* Allocate and fill vbi video_device struct */
  1626. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1627. /* register v4l2 vbi video_device */
  1628. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1629. vbi_nr[dev->devno]);
  1630. if (ret < 0) {
  1631. em28xx_errdev("unable to register vbi device\n");
  1632. return ret;
  1633. }
  1634. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1635. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
  1636. if (!dev->radio_dev) {
  1637. em28xx_errdev("cannot allocate video_device.\n");
  1638. return -ENODEV;
  1639. }
  1640. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1641. radio_nr[dev->devno]);
  1642. if (ret < 0) {
  1643. em28xx_errdev("can't register radio device\n");
  1644. return ret;
  1645. }
  1646. em28xx_info("Registered radio device as /dev/radio%d\n",
  1647. dev->radio_dev->num);
  1648. }
  1649. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1650. dev->vdev->num, dev->vbi_dev->num);
  1651. return 0;
  1652. }