em28xx-video.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  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. Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
  9. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  10. by Luca Risolia <luca.risolia@studio.unibo.it>
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/init.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/bitmap.h>
  28. #include <linux/usb.h>
  29. #include <linux/i2c.h>
  30. #include <linux/mm.h>
  31. #include <linux/mutex.h>
  32. #include <linux/slab.h>
  33. #include "em28xx.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-event.h>
  37. #include <media/v4l2-chip-ident.h>
  38. #include <media/msp3400.h>
  39. #include <media/tuner.h>
  40. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  41. "Markus Rechberger <mrechberger@gmail.com>, " \
  42. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  43. "Sascha Sommer <saschasommer@freenet.de>"
  44. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  45. #define EM28XX_VERSION "0.1.3"
  46. #define em28xx_videodbg(fmt, arg...) do {\
  47. if (video_debug) \
  48. printk(KERN_INFO "%s %s :"fmt, \
  49. dev->name, __func__ , ##arg); } while (0)
  50. static unsigned int isoc_debug;
  51. module_param(isoc_debug, int, 0644);
  52. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  53. #define em28xx_isocdbg(fmt, arg...) \
  54. do {\
  55. if (isoc_debug) { \
  56. printk(KERN_INFO "%s %s :"fmt, \
  57. dev->name, __func__ , ##arg); \
  58. } \
  59. } while (0)
  60. MODULE_AUTHOR(DRIVER_AUTHOR);
  61. MODULE_DESCRIPTION(DRIVER_DESC);
  62. MODULE_LICENSE("GPL");
  63. MODULE_VERSION(EM28XX_VERSION);
  64. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  65. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  66. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  67. module_param_array(video_nr, int, NULL, 0444);
  68. module_param_array(vbi_nr, int, NULL, 0444);
  69. module_param_array(radio_nr, int, NULL, 0444);
  70. MODULE_PARM_DESC(video_nr, "video device numbers");
  71. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  72. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  73. static unsigned int video_debug;
  74. module_param(video_debug, int, 0644);
  75. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  76. /* supported video standards */
  77. static struct em28xx_fmt format[] = {
  78. {
  79. .name = "16 bpp YUY2, 4:2:2, packed",
  80. .fourcc = V4L2_PIX_FMT_YUYV,
  81. .depth = 16,
  82. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  83. }, {
  84. .name = "16 bpp RGB 565, LE",
  85. .fourcc = V4L2_PIX_FMT_RGB565,
  86. .depth = 16,
  87. .reg = EM28XX_OUTFMT_RGB_16_656,
  88. }, {
  89. .name = "8 bpp Bayer BGBG..GRGR",
  90. .fourcc = V4L2_PIX_FMT_SBGGR8,
  91. .depth = 8,
  92. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  93. }, {
  94. .name = "8 bpp Bayer GRGR..BGBG",
  95. .fourcc = V4L2_PIX_FMT_SGRBG8,
  96. .depth = 8,
  97. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  98. }, {
  99. .name = "8 bpp Bayer GBGB..RGRG",
  100. .fourcc = V4L2_PIX_FMT_SGBRG8,
  101. .depth = 8,
  102. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  103. }, {
  104. .name = "12 bpp YUV411",
  105. .fourcc = V4L2_PIX_FMT_YUV411P,
  106. .depth = 12,
  107. .reg = EM28XX_OUTFMT_YUV411,
  108. },
  109. };
  110. /* ------------------------------------------------------------------
  111. DMA and thread functions
  112. ------------------------------------------------------------------*/
  113. /*
  114. * Finish the current buffer
  115. */
  116. static inline void finish_buffer(struct em28xx *dev,
  117. struct em28xx_buffer *buf)
  118. {
  119. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
  120. buf->vb.v4l2_buf.sequence = dev->field_count++;
  121. buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
  122. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  123. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  124. }
  125. /*
  126. * Copy picture data from USB buffer to videobuf buffer
  127. */
  128. static void em28xx_copy_video(struct em28xx *dev,
  129. struct em28xx_buffer *buf,
  130. unsigned char *usb_buf,
  131. unsigned long len)
  132. {
  133. void *fieldstart, *startwrite, *startread;
  134. int linesdone, currlinedone, offset, lencopy, remain;
  135. int bytesperline = dev->width << 1;
  136. if (buf->pos + len > buf->length)
  137. len = buf->length - buf->pos;
  138. startread = usb_buf;
  139. remain = len;
  140. if (dev->progressive || buf->top_field)
  141. fieldstart = buf->vb_buf;
  142. else /* interlaced mode, even nr. of lines */
  143. fieldstart = buf->vb_buf + bytesperline;
  144. linesdone = buf->pos / bytesperline;
  145. currlinedone = buf->pos % bytesperline;
  146. if (dev->progressive)
  147. offset = linesdone * bytesperline + currlinedone;
  148. else
  149. offset = linesdone * bytesperline * 2 + currlinedone;
  150. startwrite = fieldstart + offset;
  151. lencopy = bytesperline - currlinedone;
  152. lencopy = lencopy > remain ? remain : lencopy;
  153. if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
  154. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  155. ((char *)startwrite + lencopy) -
  156. ((char *)buf->vb_buf + buf->length));
  157. remain = (char *)buf->vb_buf + buf->length -
  158. (char *)startwrite;
  159. lencopy = remain;
  160. }
  161. if (lencopy <= 0)
  162. return;
  163. memcpy(startwrite, startread, lencopy);
  164. remain -= lencopy;
  165. while (remain > 0) {
  166. if (dev->progressive)
  167. startwrite += lencopy;
  168. else
  169. startwrite += lencopy + bytesperline;
  170. startread += lencopy;
  171. if (bytesperline > remain)
  172. lencopy = remain;
  173. else
  174. lencopy = bytesperline;
  175. if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
  176. buf->length) {
  177. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  178. "(2)\n",
  179. ((char *)startwrite + lencopy) -
  180. ((char *)buf->vb_buf + buf->length));
  181. lencopy = remain = (char *)buf->vb_buf + buf->length -
  182. (char *)startwrite;
  183. }
  184. if (lencopy <= 0)
  185. break;
  186. memcpy(startwrite, startread, lencopy);
  187. remain -= lencopy;
  188. }
  189. buf->pos += len;
  190. }
  191. /*
  192. * Copy VBI data from USB buffer to videobuf buffer
  193. */
  194. static void em28xx_copy_vbi(struct em28xx *dev,
  195. struct em28xx_buffer *buf,
  196. unsigned char *usb_buf,
  197. unsigned long len)
  198. {
  199. unsigned int offset;
  200. if (buf->pos + len > buf->length)
  201. len = buf->length - buf->pos;
  202. offset = buf->pos;
  203. /* Make sure the bottom field populates the second half of the frame */
  204. if (buf->top_field == 0)
  205. offset += dev->vbi_width * dev->vbi_height;
  206. memcpy(buf->vb_buf + offset, usb_buf, len);
  207. buf->pos += len;
  208. }
  209. static inline void print_err_status(struct em28xx *dev,
  210. int packet, int status)
  211. {
  212. char *errmsg = "Unknown";
  213. switch (status) {
  214. case -ENOENT:
  215. errmsg = "unlinked synchronuously";
  216. break;
  217. case -ECONNRESET:
  218. errmsg = "unlinked asynchronuously";
  219. break;
  220. case -ENOSR:
  221. errmsg = "Buffer error (overrun)";
  222. break;
  223. case -EPIPE:
  224. errmsg = "Stalled (device not responding)";
  225. break;
  226. case -EOVERFLOW:
  227. errmsg = "Babble (bad cable?)";
  228. break;
  229. case -EPROTO:
  230. errmsg = "Bit-stuff error (bad cable?)";
  231. break;
  232. case -EILSEQ:
  233. errmsg = "CRC/Timeout (could be anything)";
  234. break;
  235. case -ETIME:
  236. errmsg = "Device does not respond";
  237. break;
  238. }
  239. if (packet < 0) {
  240. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  241. } else {
  242. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  243. packet, status, errmsg);
  244. }
  245. }
  246. /*
  247. * get the next available buffer from dma queue
  248. */
  249. static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
  250. struct em28xx_dmaqueue *dma_q)
  251. {
  252. struct em28xx_buffer *buf;
  253. if (list_empty(&dma_q->active)) {
  254. em28xx_isocdbg("No active queue to serve\n");
  255. return NULL;
  256. }
  257. /* Get the next buffer */
  258. buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
  259. /* Cleans up buffer - Useful for testing for frame/URB loss */
  260. list_del(&buf->list);
  261. buf->pos = 0;
  262. buf->vb_buf = buf->mem;
  263. return buf;
  264. }
  265. /*
  266. * Finish the current buffer if completed and prepare for the next field
  267. */
  268. static struct em28xx_buffer *
  269. finish_field_prepare_next(struct em28xx *dev,
  270. struct em28xx_buffer *buf,
  271. struct em28xx_dmaqueue *dma_q)
  272. {
  273. if (dev->progressive || dev->top_field) { /* Brand new frame */
  274. if (buf != NULL)
  275. finish_buffer(dev, buf);
  276. buf = get_next_buf(dev, dma_q);
  277. }
  278. if (buf != NULL) {
  279. buf->top_field = dev->top_field;
  280. buf->pos = 0;
  281. }
  282. return buf;
  283. }
  284. /*
  285. * Process data packet according to the em2710/em2750/em28xx frame data format
  286. */
  287. static inline void process_frame_data_em28xx(struct em28xx *dev,
  288. unsigned char *data_pkt,
  289. unsigned int data_len)
  290. {
  291. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  292. struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
  293. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  294. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  295. /* capture type 0 = vbi start
  296. capture type 1 = vbi in progress
  297. capture type 2 = video start
  298. capture type 3 = video in progress */
  299. if (data_len >= 4) {
  300. /* NOTE: Headers are always 4 bytes and
  301. * never split across packets */
  302. if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
  303. data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
  304. /* Continuation */
  305. data_pkt += 4;
  306. data_len -= 4;
  307. } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
  308. /* Field start (VBI mode) */
  309. dev->capture_type = 0;
  310. dev->vbi_read = 0;
  311. em28xx_isocdbg("VBI START HEADER !!!\n");
  312. dev->top_field = !(data_pkt[2] & 1);
  313. data_pkt += 4;
  314. data_len -= 4;
  315. } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
  316. /* Field start (VBI disabled) */
  317. dev->capture_type = 2;
  318. em28xx_isocdbg("VIDEO START HEADER !!!\n");
  319. dev->top_field = !(data_pkt[2] & 1);
  320. data_pkt += 4;
  321. data_len -= 4;
  322. }
  323. }
  324. /* NOTE: With bulk transfers, intermediate data packets
  325. * have no continuation header */
  326. if (dev->capture_type == 0) {
  327. vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
  328. dev->usb_ctl.vbi_buf = vbi_buf;
  329. dev->capture_type = 1;
  330. }
  331. if (dev->capture_type == 1) {
  332. int vbi_size = dev->vbi_width * dev->vbi_height;
  333. int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
  334. (vbi_size - dev->vbi_read) : data_len;
  335. /* Copy VBI data */
  336. if (vbi_buf != NULL)
  337. em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
  338. dev->vbi_read += vbi_data_len;
  339. if (vbi_data_len < data_len) {
  340. /* Continue with copying video data */
  341. dev->capture_type = 2;
  342. data_pkt += vbi_data_len;
  343. data_len -= vbi_data_len;
  344. }
  345. }
  346. if (dev->capture_type == 2) {
  347. buf = finish_field_prepare_next(dev, buf, dma_q);
  348. dev->usb_ctl.vid_buf = buf;
  349. dev->capture_type = 3;
  350. }
  351. if (dev->capture_type == 3 && buf != NULL && data_len > 0)
  352. em28xx_copy_video(dev, buf, data_pkt, data_len);
  353. }
  354. /* Processes and copies the URB data content (video and VBI data) */
  355. static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
  356. {
  357. int xfer_bulk, num_packets, i;
  358. unsigned char *usb_data_pkt;
  359. unsigned int usb_data_len;
  360. if (!dev)
  361. return 0;
  362. if (dev->disconnected)
  363. return 0;
  364. if (urb->status < 0)
  365. print_err_status(dev, -1, urb->status);
  366. xfer_bulk = usb_pipebulk(urb->pipe);
  367. if (xfer_bulk) /* bulk */
  368. num_packets = 1;
  369. else /* isoc */
  370. num_packets = urb->number_of_packets;
  371. for (i = 0; i < num_packets; i++) {
  372. if (xfer_bulk) { /* bulk */
  373. usb_data_len = urb->actual_length;
  374. usb_data_pkt = urb->transfer_buffer;
  375. } else { /* isoc */
  376. if (urb->iso_frame_desc[i].status < 0) {
  377. print_err_status(dev, i,
  378. urb->iso_frame_desc[i].status);
  379. if (urb->iso_frame_desc[i].status != -EPROTO)
  380. continue;
  381. }
  382. usb_data_len = urb->iso_frame_desc[i].actual_length;
  383. if (usb_data_len > dev->max_pkt_size) {
  384. em28xx_isocdbg("packet bigger than packet size");
  385. continue;
  386. }
  387. usb_data_pkt = urb->transfer_buffer +
  388. urb->iso_frame_desc[i].offset;
  389. }
  390. if (usb_data_len == 0) {
  391. /* NOTE: happens very often with isoc transfers */
  392. /* em28xx_usbdbg("packet %d is empty",i); - spammy */
  393. continue;
  394. }
  395. process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
  396. }
  397. return 1;
  398. }
  399. static int get_ressource(enum v4l2_buf_type f_type)
  400. {
  401. switch (f_type) {
  402. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  403. return EM28XX_RESOURCE_VIDEO;
  404. case V4L2_BUF_TYPE_VBI_CAPTURE:
  405. return EM28XX_RESOURCE_VBI;
  406. default:
  407. BUG();
  408. return 0;
  409. }
  410. }
  411. /* Usage lock check functions */
  412. static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
  413. {
  414. int res_type = get_ressource(f_type);
  415. /* is it free? */
  416. if (dev->resources & res_type) {
  417. /* no, someone else uses it */
  418. return -EBUSY;
  419. }
  420. /* it's free, grab it */
  421. dev->resources |= res_type;
  422. em28xx_videodbg("res: get %d\n", res_type);
  423. return 0;
  424. }
  425. static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
  426. {
  427. int res_type = get_ressource(f_type);
  428. dev->resources &= ~res_type;
  429. em28xx_videodbg("res: put %d\n", res_type);
  430. }
  431. /* ------------------------------------------------------------------
  432. Videobuf2 operations
  433. ------------------------------------------------------------------*/
  434. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  435. unsigned int *nbuffers, unsigned int *nplanes,
  436. unsigned int sizes[], void *alloc_ctxs[])
  437. {
  438. struct em28xx *dev = vb2_get_drv_priv(vq);
  439. unsigned long size;
  440. if (fmt)
  441. size = fmt->fmt.pix.sizeimage;
  442. else
  443. size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
  444. if (size == 0)
  445. return -EINVAL;
  446. if (0 == *nbuffers)
  447. *nbuffers = 32;
  448. *nplanes = 1;
  449. sizes[0] = size;
  450. return 0;
  451. }
  452. static int
  453. buffer_prepare(struct vb2_buffer *vb)
  454. {
  455. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  456. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  457. unsigned long size;
  458. em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
  459. size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
  460. if (vb2_plane_size(vb, 0) < size) {
  461. em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
  462. __func__, vb2_plane_size(vb, 0), size);
  463. return -EINVAL;
  464. }
  465. vb2_set_plane_payload(&buf->vb, 0, size);
  466. return 0;
  467. }
  468. int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
  469. {
  470. struct em28xx *dev = vb2_get_drv_priv(vq);
  471. struct v4l2_frequency f;
  472. int rc = 0;
  473. em28xx_videodbg("%s\n", __func__);
  474. /* Make sure streaming is not already in progress for this type
  475. of filehandle (e.g. video, vbi) */
  476. rc = res_get(dev, vq->type);
  477. if (rc)
  478. return rc;
  479. if (dev->streaming_users++ == 0) {
  480. /* First active streaming user, so allocate all the URBs */
  481. /* Allocate the USB bandwidth */
  482. em28xx_set_alternate(dev);
  483. /* Needed, since GPIO might have disabled power of
  484. some i2c device
  485. */
  486. em28xx_wake_i2c(dev);
  487. dev->capture_type = -1;
  488. rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
  489. dev->analog_xfer_bulk,
  490. EM28XX_NUM_BUFS,
  491. dev->max_pkt_size,
  492. dev->packet_multiplier,
  493. em28xx_urb_data_copy);
  494. if (rc < 0)
  495. goto fail;
  496. /*
  497. * djh: it's not clear whether this code is still needed. I'm
  498. * leaving it in here for now entirely out of concern for
  499. * backward compatibility (the old code did it)
  500. */
  501. /* Ask tuner to go to analog or radio mode */
  502. memset(&f, 0, sizeof(f));
  503. f.frequency = dev->ctl_freq;
  504. if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
  505. f.type = V4L2_TUNER_RADIO;
  506. else
  507. f.type = V4L2_TUNER_ANALOG_TV;
  508. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  509. }
  510. fail:
  511. return rc;
  512. }
  513. static int em28xx_stop_streaming(struct vb2_queue *vq)
  514. {
  515. struct em28xx *dev = vb2_get_drv_priv(vq);
  516. struct em28xx_dmaqueue *vidq = &dev->vidq;
  517. unsigned long flags = 0;
  518. em28xx_videodbg("%s\n", __func__);
  519. res_free(dev, vq->type);
  520. if (dev->streaming_users-- == 1) {
  521. /* Last active user, so shutdown all the URBS */
  522. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  523. }
  524. spin_lock_irqsave(&dev->slock, flags);
  525. while (!list_empty(&vidq->active)) {
  526. struct em28xx_buffer *buf;
  527. buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
  528. list_del(&buf->list);
  529. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  530. }
  531. dev->usb_ctl.vid_buf = NULL;
  532. spin_unlock_irqrestore(&dev->slock, flags);
  533. return 0;
  534. }
  535. int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
  536. {
  537. struct em28xx *dev = vb2_get_drv_priv(vq);
  538. struct em28xx_dmaqueue *vbiq = &dev->vbiq;
  539. unsigned long flags = 0;
  540. em28xx_videodbg("%s\n", __func__);
  541. res_free(dev, vq->type);
  542. if (dev->streaming_users-- == 1) {
  543. /* Last active user, so shutdown all the URBS */
  544. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  545. }
  546. spin_lock_irqsave(&dev->slock, flags);
  547. while (!list_empty(&vbiq->active)) {
  548. struct em28xx_buffer *buf;
  549. buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
  550. list_del(&buf->list);
  551. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  552. }
  553. dev->usb_ctl.vbi_buf = NULL;
  554. spin_unlock_irqrestore(&dev->slock, flags);
  555. return 0;
  556. }
  557. static void
  558. buffer_queue(struct vb2_buffer *vb)
  559. {
  560. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  561. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  562. struct em28xx_dmaqueue *vidq = &dev->vidq;
  563. unsigned long flags = 0;
  564. em28xx_videodbg("%s\n", __func__);
  565. buf->mem = vb2_plane_vaddr(vb, 0);
  566. buf->length = vb2_plane_size(vb, 0);
  567. spin_lock_irqsave(&dev->slock, flags);
  568. list_add_tail(&buf->list, &vidq->active);
  569. spin_unlock_irqrestore(&dev->slock, flags);
  570. }
  571. static struct vb2_ops em28xx_video_qops = {
  572. .queue_setup = queue_setup,
  573. .buf_prepare = buffer_prepare,
  574. .buf_queue = buffer_queue,
  575. .start_streaming = em28xx_start_analog_streaming,
  576. .stop_streaming = em28xx_stop_streaming,
  577. .wait_prepare = vb2_ops_wait_prepare,
  578. .wait_finish = vb2_ops_wait_finish,
  579. };
  580. int em28xx_vb2_setup(struct em28xx *dev)
  581. {
  582. int rc;
  583. struct vb2_queue *q;
  584. /* Setup Videobuf2 for Video capture */
  585. q = &dev->vb_vidq;
  586. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  587. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  588. q->drv_priv = dev;
  589. q->buf_struct_size = sizeof(struct em28xx_buffer);
  590. q->ops = &em28xx_video_qops;
  591. q->mem_ops = &vb2_vmalloc_memops;
  592. rc = vb2_queue_init(q);
  593. if (rc < 0)
  594. return rc;
  595. /* Setup Videobuf2 for VBI capture */
  596. q = &dev->vb_vbiq;
  597. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  598. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  599. q->drv_priv = dev;
  600. q->buf_struct_size = sizeof(struct em28xx_buffer);
  601. q->ops = &em28xx_vbi_qops;
  602. q->mem_ops = &vb2_vmalloc_memops;
  603. rc = vb2_queue_init(q);
  604. if (rc < 0)
  605. return rc;
  606. return 0;
  607. }
  608. /********************* v4l2 interface **************************************/
  609. static void video_mux(struct em28xx *dev, int index)
  610. {
  611. dev->ctl_input = index;
  612. dev->ctl_ainput = INPUT(index)->amux;
  613. dev->ctl_aoutput = INPUT(index)->aout;
  614. if (!dev->ctl_aoutput)
  615. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  616. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  617. INPUT(index)->vmux, 0, 0);
  618. if (dev->board.has_msp34xx) {
  619. if (dev->i2s_speed) {
  620. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  621. s_i2s_clock_freq, dev->i2s_speed);
  622. }
  623. /* Note: this is msp3400 specific */
  624. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  625. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  626. }
  627. if (dev->board.adecoder != EM28XX_NOADECODER) {
  628. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  629. dev->ctl_ainput, dev->ctl_aoutput, 0);
  630. }
  631. em28xx_audio_analog_set(dev);
  632. }
  633. void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
  634. {
  635. struct em28xx *dev = priv;
  636. /*
  637. * In the case of non-AC97 volume controls, we still need
  638. * to do some setups at em28xx, in order to mute/unmute
  639. * and to adjust audio volume. However, the value ranges
  640. * should be checked by the corresponding V4L subdriver.
  641. */
  642. switch (ctrl->id) {
  643. case V4L2_CID_AUDIO_MUTE:
  644. dev->mute = ctrl->val;
  645. em28xx_audio_analog_set(dev);
  646. break;
  647. case V4L2_CID_AUDIO_VOLUME:
  648. dev->volume = ctrl->val;
  649. em28xx_audio_analog_set(dev);
  650. break;
  651. }
  652. }
  653. static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
  654. {
  655. struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
  656. switch (ctrl->id) {
  657. case V4L2_CID_AUDIO_MUTE:
  658. dev->mute = ctrl->val;
  659. break;
  660. case V4L2_CID_AUDIO_VOLUME:
  661. dev->volume = ctrl->val;
  662. break;
  663. }
  664. return em28xx_audio_analog_set(dev);
  665. }
  666. const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
  667. .s_ctrl = em28xx_s_ctrl,
  668. };
  669. static int check_dev(struct em28xx *dev)
  670. {
  671. if (dev->disconnected) {
  672. em28xx_errdev("v4l2 ioctl: device not present\n");
  673. return -ENODEV;
  674. }
  675. return 0;
  676. }
  677. static void get_scale(struct em28xx *dev,
  678. unsigned int width, unsigned int height,
  679. unsigned int *hscale, unsigned int *vscale)
  680. {
  681. unsigned int maxw = norm_maxw(dev);
  682. unsigned int maxh = norm_maxh(dev);
  683. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  684. if (*hscale >= 0x4000)
  685. *hscale = 0x3fff;
  686. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  687. if (*vscale >= 0x4000)
  688. *vscale = 0x3fff;
  689. }
  690. /* ------------------------------------------------------------------
  691. IOCTL vidioc handling
  692. ------------------------------------------------------------------*/
  693. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  694. struct v4l2_format *f)
  695. {
  696. struct em28xx_fh *fh = priv;
  697. struct em28xx *dev = fh->dev;
  698. f->fmt.pix.width = dev->width;
  699. f->fmt.pix.height = dev->height;
  700. f->fmt.pix.pixelformat = dev->format->fourcc;
  701. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  702. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  703. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  704. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  705. if (dev->progressive)
  706. f->fmt.pix.field = V4L2_FIELD_NONE;
  707. else
  708. f->fmt.pix.field = dev->interlaced ?
  709. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  710. return 0;
  711. }
  712. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  713. {
  714. unsigned int i;
  715. for (i = 0; i < ARRAY_SIZE(format); i++)
  716. if (format[i].fourcc == fourcc)
  717. return &format[i];
  718. return NULL;
  719. }
  720. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  721. struct v4l2_format *f)
  722. {
  723. struct em28xx_fh *fh = priv;
  724. struct em28xx *dev = fh->dev;
  725. unsigned int width = f->fmt.pix.width;
  726. unsigned int height = f->fmt.pix.height;
  727. unsigned int maxw = norm_maxw(dev);
  728. unsigned int maxh = norm_maxh(dev);
  729. unsigned int hscale, vscale;
  730. struct em28xx_fmt *fmt;
  731. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  732. if (!fmt) {
  733. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  734. f->fmt.pix.pixelformat);
  735. return -EINVAL;
  736. }
  737. if (dev->board.is_em2800) {
  738. /* the em2800 can only scale down to 50% */
  739. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  740. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  741. /*
  742. * MaxPacketSize for em2800 is too small to capture at full
  743. * resolution use half of maxw as the scaler can only scale
  744. * to 50%
  745. */
  746. if (width == maxw && height == maxh)
  747. width /= 2;
  748. } else {
  749. /* width must even because of the YUYV format
  750. height must be even because of interlacing */
  751. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  752. 1, 0);
  753. }
  754. get_scale(dev, width, height, &hscale, &vscale);
  755. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  756. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  757. f->fmt.pix.width = width;
  758. f->fmt.pix.height = height;
  759. f->fmt.pix.pixelformat = fmt->fourcc;
  760. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  761. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  762. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  763. if (dev->progressive)
  764. f->fmt.pix.field = V4L2_FIELD_NONE;
  765. else
  766. f->fmt.pix.field = dev->interlaced ?
  767. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  768. return 0;
  769. }
  770. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  771. unsigned width, unsigned height)
  772. {
  773. struct em28xx_fmt *fmt;
  774. fmt = format_by_fourcc(fourcc);
  775. if (!fmt)
  776. return -EINVAL;
  777. dev->format = fmt;
  778. dev->width = width;
  779. dev->height = height;
  780. /* set new image size */
  781. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  782. em28xx_resolution_set(dev);
  783. return 0;
  784. }
  785. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  786. struct v4l2_format *f)
  787. {
  788. struct em28xx *dev = video_drvdata(file);
  789. if (dev->streaming_users > 0)
  790. return -EBUSY;
  791. vidioc_try_fmt_vid_cap(file, priv, f);
  792. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  793. f->fmt.pix.width, f->fmt.pix.height);
  794. }
  795. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  796. {
  797. struct em28xx_fh *fh = priv;
  798. struct em28xx *dev = fh->dev;
  799. int rc;
  800. if (dev->board.is_webcam)
  801. return -ENOTTY;
  802. rc = check_dev(dev);
  803. if (rc < 0)
  804. return rc;
  805. *norm = dev->norm;
  806. return 0;
  807. }
  808. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  809. {
  810. struct em28xx_fh *fh = priv;
  811. struct em28xx *dev = fh->dev;
  812. int rc;
  813. if (dev->board.is_webcam)
  814. return -ENOTTY;
  815. rc = check_dev(dev);
  816. if (rc < 0)
  817. return rc;
  818. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  819. return 0;
  820. }
  821. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  822. {
  823. struct em28xx_fh *fh = priv;
  824. struct em28xx *dev = fh->dev;
  825. struct v4l2_format f;
  826. int rc;
  827. if (dev->board.is_webcam)
  828. return -ENOTTY;
  829. if (*norm == dev->norm)
  830. return 0;
  831. rc = check_dev(dev);
  832. if (rc < 0)
  833. return rc;
  834. if (dev->streaming_users > 0)
  835. return -EBUSY;
  836. dev->norm = *norm;
  837. /* Adjusts width/height, if needed */
  838. f.fmt.pix.width = 720;
  839. f.fmt.pix.height = (*norm & V4L2_STD_525_60) ? 480 : 576;
  840. vidioc_try_fmt_vid_cap(file, priv, &f);
  841. /* set new image size */
  842. dev->width = f.fmt.pix.width;
  843. dev->height = f.fmt.pix.height;
  844. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  845. em28xx_resolution_set(dev);
  846. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  847. return 0;
  848. }
  849. static int vidioc_g_parm(struct file *file, void *priv,
  850. struct v4l2_streamparm *p)
  851. {
  852. struct em28xx_fh *fh = priv;
  853. struct em28xx *dev = fh->dev;
  854. int rc = 0;
  855. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  856. return -EINVAL;
  857. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  858. if (dev->board.is_webcam)
  859. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  860. video, g_parm, p);
  861. else
  862. v4l2_video_std_frame_period(dev->norm,
  863. &p->parm.capture.timeperframe);
  864. return rc;
  865. }
  866. static int vidioc_s_parm(struct file *file, void *priv,
  867. struct v4l2_streamparm *p)
  868. {
  869. struct em28xx_fh *fh = priv;
  870. struct em28xx *dev = fh->dev;
  871. if (!dev->board.is_webcam)
  872. return -ENOTTY;
  873. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  874. return -EINVAL;
  875. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  876. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  877. }
  878. static const char *iname[] = {
  879. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  880. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  881. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  882. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  883. [EM28XX_VMUX_SVIDEO] = "S-Video",
  884. [EM28XX_VMUX_TELEVISION] = "Television",
  885. [EM28XX_VMUX_CABLE] = "Cable TV",
  886. [EM28XX_VMUX_DVB] = "DVB",
  887. [EM28XX_VMUX_DEBUG] = "for debug only",
  888. };
  889. static int vidioc_enum_input(struct file *file, void *priv,
  890. struct v4l2_input *i)
  891. {
  892. struct em28xx_fh *fh = priv;
  893. struct em28xx *dev = fh->dev;
  894. unsigned int n;
  895. n = i->index;
  896. if (n >= MAX_EM28XX_INPUT)
  897. return -EINVAL;
  898. if (0 == INPUT(n)->type)
  899. return -EINVAL;
  900. i->index = n;
  901. i->type = V4L2_INPUT_TYPE_CAMERA;
  902. strcpy(i->name, iname[INPUT(n)->type]);
  903. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  904. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  905. i->type = V4L2_INPUT_TYPE_TUNER;
  906. i->std = dev->vdev->tvnorms;
  907. /* webcams do not have the STD API */
  908. if (dev->board.is_webcam)
  909. i->capabilities = 0;
  910. return 0;
  911. }
  912. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  913. {
  914. struct em28xx_fh *fh = priv;
  915. struct em28xx *dev = fh->dev;
  916. *i = dev->ctl_input;
  917. return 0;
  918. }
  919. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  920. {
  921. struct em28xx_fh *fh = priv;
  922. struct em28xx *dev = fh->dev;
  923. int rc;
  924. rc = check_dev(dev);
  925. if (rc < 0)
  926. return rc;
  927. if (i >= MAX_EM28XX_INPUT)
  928. return -EINVAL;
  929. if (0 == INPUT(i)->type)
  930. return -EINVAL;
  931. video_mux(dev, i);
  932. return 0;
  933. }
  934. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  935. {
  936. struct em28xx_fh *fh = priv;
  937. struct em28xx *dev = fh->dev;
  938. if (!dev->audio_mode.has_audio)
  939. return -EINVAL;
  940. switch (a->index) {
  941. case EM28XX_AMUX_VIDEO:
  942. strcpy(a->name, "Television");
  943. break;
  944. case EM28XX_AMUX_LINE_IN:
  945. strcpy(a->name, "Line In");
  946. break;
  947. case EM28XX_AMUX_VIDEO2:
  948. strcpy(a->name, "Television alt");
  949. break;
  950. case EM28XX_AMUX_PHONE:
  951. strcpy(a->name, "Phone");
  952. break;
  953. case EM28XX_AMUX_MIC:
  954. strcpy(a->name, "Mic");
  955. break;
  956. case EM28XX_AMUX_CD:
  957. strcpy(a->name, "CD");
  958. break;
  959. case EM28XX_AMUX_AUX:
  960. strcpy(a->name, "Aux");
  961. break;
  962. case EM28XX_AMUX_PCM_OUT:
  963. strcpy(a->name, "PCM");
  964. break;
  965. default:
  966. return -EINVAL;
  967. }
  968. a->index = dev->ctl_ainput;
  969. a->capability = V4L2_AUDCAP_STEREO;
  970. return 0;
  971. }
  972. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  973. {
  974. struct em28xx_fh *fh = priv;
  975. struct em28xx *dev = fh->dev;
  976. if (!dev->audio_mode.has_audio)
  977. return -EINVAL;
  978. if (a->index >= MAX_EM28XX_INPUT)
  979. return -EINVAL;
  980. if (0 == INPUT(a->index)->type)
  981. return -EINVAL;
  982. dev->ctl_ainput = INPUT(a->index)->amux;
  983. dev->ctl_aoutput = INPUT(a->index)->aout;
  984. if (!dev->ctl_aoutput)
  985. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  986. return 0;
  987. }
  988. static int vidioc_g_tuner(struct file *file, void *priv,
  989. struct v4l2_tuner *t)
  990. {
  991. struct em28xx_fh *fh = priv;
  992. struct em28xx *dev = fh->dev;
  993. int rc;
  994. rc = check_dev(dev);
  995. if (rc < 0)
  996. return rc;
  997. if (0 != t->index)
  998. return -EINVAL;
  999. strcpy(t->name, "Tuner");
  1000. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1001. return 0;
  1002. }
  1003. static int vidioc_s_tuner(struct file *file, void *priv,
  1004. struct v4l2_tuner *t)
  1005. {
  1006. struct em28xx_fh *fh = priv;
  1007. struct em28xx *dev = fh->dev;
  1008. int rc;
  1009. rc = check_dev(dev);
  1010. if (rc < 0)
  1011. return rc;
  1012. if (0 != t->index)
  1013. return -EINVAL;
  1014. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1015. return 0;
  1016. }
  1017. static int vidioc_g_frequency(struct file *file, void *priv,
  1018. struct v4l2_frequency *f)
  1019. {
  1020. struct em28xx_fh *fh = priv;
  1021. struct em28xx *dev = fh->dev;
  1022. if (0 != f->tuner)
  1023. return -EINVAL;
  1024. f->frequency = dev->ctl_freq;
  1025. return 0;
  1026. }
  1027. static int vidioc_s_frequency(struct file *file, void *priv,
  1028. struct v4l2_frequency *f)
  1029. {
  1030. struct em28xx_fh *fh = priv;
  1031. struct em28xx *dev = fh->dev;
  1032. int rc;
  1033. rc = check_dev(dev);
  1034. if (rc < 0)
  1035. return rc;
  1036. if (0 != f->tuner)
  1037. return -EINVAL;
  1038. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1039. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  1040. dev->ctl_freq = f->frequency;
  1041. return 0;
  1042. }
  1043. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1044. static int em28xx_reg_len(int reg)
  1045. {
  1046. switch (reg) {
  1047. case EM28XX_R40_AC97LSB:
  1048. case EM28XX_R30_HSCALELOW:
  1049. case EM28XX_R32_VSCALELOW:
  1050. return 2;
  1051. default:
  1052. return 1;
  1053. }
  1054. }
  1055. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1056. struct v4l2_dbg_chip_ident *chip)
  1057. {
  1058. struct em28xx_fh *fh = priv;
  1059. struct em28xx *dev = fh->dev;
  1060. chip->ident = V4L2_IDENT_NONE;
  1061. chip->revision = 0;
  1062. if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
  1063. if (v4l2_chip_match_host(&chip->match))
  1064. chip->ident = V4L2_IDENT_NONE;
  1065. return 0;
  1066. }
  1067. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1068. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  1069. return -EINVAL;
  1070. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1071. return 0;
  1072. }
  1073. static int vidioc_g_register(struct file *file, void *priv,
  1074. struct v4l2_dbg_register *reg)
  1075. {
  1076. struct em28xx_fh *fh = priv;
  1077. struct em28xx *dev = fh->dev;
  1078. int ret;
  1079. switch (reg->match.type) {
  1080. case V4L2_CHIP_MATCH_AC97:
  1081. ret = em28xx_read_ac97(dev, reg->reg);
  1082. if (ret < 0)
  1083. return ret;
  1084. reg->val = ret;
  1085. reg->size = 1;
  1086. return 0;
  1087. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1088. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1089. return 0;
  1090. case V4L2_CHIP_MATCH_I2C_ADDR:
  1091. /* TODO: is this correct? */
  1092. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1093. return 0;
  1094. default:
  1095. if (!v4l2_chip_match_host(&reg->match))
  1096. return -EINVAL;
  1097. }
  1098. /* Match host */
  1099. reg->size = em28xx_reg_len(reg->reg);
  1100. if (reg->size == 1) {
  1101. ret = em28xx_read_reg(dev, reg->reg);
  1102. if (ret < 0)
  1103. return ret;
  1104. reg->val = ret;
  1105. } else {
  1106. __le16 val = 0;
  1107. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1108. reg->reg, (char *)&val, 2);
  1109. if (ret < 0)
  1110. return ret;
  1111. reg->val = le16_to_cpu(val);
  1112. }
  1113. return 0;
  1114. }
  1115. static int vidioc_s_register(struct file *file, void *priv,
  1116. struct v4l2_dbg_register *reg)
  1117. {
  1118. struct em28xx_fh *fh = priv;
  1119. struct em28xx *dev = fh->dev;
  1120. __le16 buf;
  1121. switch (reg->match.type) {
  1122. case V4L2_CHIP_MATCH_AC97:
  1123. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1124. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1125. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1126. return 0;
  1127. case V4L2_CHIP_MATCH_I2C_ADDR:
  1128. /* TODO: is this correct? */
  1129. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1130. return 0;
  1131. default:
  1132. if (!v4l2_chip_match_host(&reg->match))
  1133. return -EINVAL;
  1134. }
  1135. /* Match host */
  1136. buf = cpu_to_le16(reg->val);
  1137. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1138. em28xx_reg_len(reg->reg));
  1139. }
  1140. #endif
  1141. static int vidioc_cropcap(struct file *file, void *priv,
  1142. struct v4l2_cropcap *cc)
  1143. {
  1144. struct em28xx_fh *fh = priv;
  1145. struct em28xx *dev = fh->dev;
  1146. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1147. return -EINVAL;
  1148. cc->bounds.left = 0;
  1149. cc->bounds.top = 0;
  1150. cc->bounds.width = dev->width;
  1151. cc->bounds.height = dev->height;
  1152. cc->defrect = cc->bounds;
  1153. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1154. cc->pixelaspect.denominator = 59;
  1155. return 0;
  1156. }
  1157. static int vidioc_querycap(struct file *file, void *priv,
  1158. struct v4l2_capability *cap)
  1159. {
  1160. struct video_device *vdev = video_devdata(file);
  1161. struct em28xx_fh *fh = priv;
  1162. struct em28xx *dev = fh->dev;
  1163. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1164. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1165. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1166. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1167. cap->device_caps = V4L2_CAP_READWRITE |
  1168. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1169. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1170. cap->device_caps = V4L2_CAP_RADIO;
  1171. else
  1172. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  1173. if (dev->audio_mode.has_audio)
  1174. cap->device_caps |= V4L2_CAP_AUDIO;
  1175. if (dev->tuner_type != TUNER_ABSENT)
  1176. cap->device_caps |= V4L2_CAP_TUNER;
  1177. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  1178. V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1179. if (dev->vbi_dev)
  1180. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1181. if (dev->radio_dev)
  1182. cap->capabilities |= V4L2_CAP_RADIO;
  1183. return 0;
  1184. }
  1185. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1186. struct v4l2_fmtdesc *f)
  1187. {
  1188. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1189. return -EINVAL;
  1190. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1191. f->pixelformat = format[f->index].fourcc;
  1192. return 0;
  1193. }
  1194. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1195. struct v4l2_frmsizeenum *fsize)
  1196. {
  1197. struct em28xx_fh *fh = priv;
  1198. struct em28xx *dev = fh->dev;
  1199. struct em28xx_fmt *fmt;
  1200. unsigned int maxw = norm_maxw(dev);
  1201. unsigned int maxh = norm_maxh(dev);
  1202. fmt = format_by_fourcc(fsize->pixel_format);
  1203. if (!fmt) {
  1204. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1205. fsize->pixel_format);
  1206. return -EINVAL;
  1207. }
  1208. if (dev->board.is_em2800) {
  1209. if (fsize->index > 1)
  1210. return -EINVAL;
  1211. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1212. fsize->discrete.width = maxw / (1 + fsize->index);
  1213. fsize->discrete.height = maxh / (1 + fsize->index);
  1214. return 0;
  1215. }
  1216. if (fsize->index != 0)
  1217. return -EINVAL;
  1218. /* Report a continuous range */
  1219. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1220. fsize->stepwise.min_width = 48;
  1221. fsize->stepwise.min_height = 32;
  1222. fsize->stepwise.max_width = maxw;
  1223. fsize->stepwise.max_height = maxh;
  1224. fsize->stepwise.step_width = 1;
  1225. fsize->stepwise.step_height = 1;
  1226. return 0;
  1227. }
  1228. /* RAW VBI ioctls */
  1229. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1230. struct v4l2_format *format)
  1231. {
  1232. struct em28xx_fh *fh = priv;
  1233. struct em28xx *dev = fh->dev;
  1234. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1235. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1236. format->fmt.vbi.offset = 0;
  1237. format->fmt.vbi.flags = 0;
  1238. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1239. format->fmt.vbi.count[0] = dev->vbi_height;
  1240. format->fmt.vbi.count[1] = dev->vbi_height;
  1241. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1242. /* Varies by video standard (NTSC, PAL, etc.) */
  1243. if (dev->norm & V4L2_STD_525_60) {
  1244. /* NTSC */
  1245. format->fmt.vbi.start[0] = 10;
  1246. format->fmt.vbi.start[1] = 273;
  1247. } else if (dev->norm & V4L2_STD_625_50) {
  1248. /* PAL */
  1249. format->fmt.vbi.start[0] = 6;
  1250. format->fmt.vbi.start[1] = 318;
  1251. }
  1252. return 0;
  1253. }
  1254. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1255. struct v4l2_format *format)
  1256. {
  1257. struct em28xx_fh *fh = priv;
  1258. struct em28xx *dev = fh->dev;
  1259. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1260. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1261. format->fmt.vbi.offset = 0;
  1262. format->fmt.vbi.flags = 0;
  1263. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1264. format->fmt.vbi.count[0] = dev->vbi_height;
  1265. format->fmt.vbi.count[1] = dev->vbi_height;
  1266. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1267. /* Varies by video standard (NTSC, PAL, etc.) */
  1268. if (dev->norm & V4L2_STD_525_60) {
  1269. /* NTSC */
  1270. format->fmt.vbi.start[0] = 10;
  1271. format->fmt.vbi.start[1] = 273;
  1272. } else if (dev->norm & V4L2_STD_625_50) {
  1273. /* PAL */
  1274. format->fmt.vbi.start[0] = 6;
  1275. format->fmt.vbi.start[1] = 318;
  1276. }
  1277. return 0;
  1278. }
  1279. /* ----------------------------------------------------------- */
  1280. /* RADIO ESPECIFIC IOCTLS */
  1281. /* ----------------------------------------------------------- */
  1282. static int radio_g_tuner(struct file *file, void *priv,
  1283. struct v4l2_tuner *t)
  1284. {
  1285. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1286. if (unlikely(t->index > 0))
  1287. return -EINVAL;
  1288. strcpy(t->name, "Radio");
  1289. t->type = V4L2_TUNER_RADIO;
  1290. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1291. return 0;
  1292. }
  1293. static int radio_s_tuner(struct file *file, void *priv,
  1294. struct v4l2_tuner *t)
  1295. {
  1296. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1297. if (0 != t->index)
  1298. return -EINVAL;
  1299. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1300. return 0;
  1301. }
  1302. /*
  1303. * em28xx_v4l2_open()
  1304. * inits the device and starts isoc transfer
  1305. */
  1306. static int em28xx_v4l2_open(struct file *filp)
  1307. {
  1308. struct video_device *vdev = video_devdata(filp);
  1309. struct em28xx *dev = video_drvdata(filp);
  1310. enum v4l2_buf_type fh_type = 0;
  1311. struct em28xx_fh *fh;
  1312. switch (vdev->vfl_type) {
  1313. case VFL_TYPE_GRABBER:
  1314. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1315. break;
  1316. case VFL_TYPE_VBI:
  1317. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1318. break;
  1319. }
  1320. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1321. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1322. dev->users);
  1323. if (mutex_lock_interruptible(&dev->lock))
  1324. return -ERESTARTSYS;
  1325. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1326. if (!fh) {
  1327. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1328. mutex_unlock(&dev->lock);
  1329. return -ENOMEM;
  1330. }
  1331. v4l2_fh_init(&fh->fh, vdev);
  1332. fh->dev = dev;
  1333. fh->type = fh_type;
  1334. filp->private_data = fh;
  1335. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1336. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1337. em28xx_resolution_set(dev);
  1338. /* Needed, since GPIO might have disabled power of
  1339. some i2c device
  1340. */
  1341. em28xx_wake_i2c(dev);
  1342. }
  1343. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1344. em28xx_videodbg("video_open: setting radio device\n");
  1345. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1346. }
  1347. dev->users++;
  1348. mutex_unlock(&dev->lock);
  1349. v4l2_fh_add(&fh->fh);
  1350. return 0;
  1351. }
  1352. /*
  1353. * em28xx_realease_resources()
  1354. * unregisters the v4l2,i2c and usb devices
  1355. * called when the device gets disconected or at module unload
  1356. */
  1357. void em28xx_release_analog_resources(struct em28xx *dev)
  1358. {
  1359. /*FIXME: I2C IR should be disconnected */
  1360. if (dev->radio_dev) {
  1361. if (video_is_registered(dev->radio_dev))
  1362. video_unregister_device(dev->radio_dev);
  1363. else
  1364. video_device_release(dev->radio_dev);
  1365. dev->radio_dev = NULL;
  1366. }
  1367. if (dev->vbi_dev) {
  1368. em28xx_info("V4L2 device %s deregistered\n",
  1369. video_device_node_name(dev->vbi_dev));
  1370. if (video_is_registered(dev->vbi_dev))
  1371. video_unregister_device(dev->vbi_dev);
  1372. else
  1373. video_device_release(dev->vbi_dev);
  1374. dev->vbi_dev = NULL;
  1375. }
  1376. if (dev->vdev) {
  1377. em28xx_info("V4L2 device %s deregistered\n",
  1378. video_device_node_name(dev->vdev));
  1379. if (video_is_registered(dev->vdev))
  1380. video_unregister_device(dev->vdev);
  1381. else
  1382. video_device_release(dev->vdev);
  1383. dev->vdev = NULL;
  1384. }
  1385. }
  1386. /*
  1387. * em28xx_v4l2_close()
  1388. * stops streaming and deallocates all resources allocated by the v4l2
  1389. * calls and ioctls
  1390. */
  1391. static int em28xx_v4l2_close(struct file *filp)
  1392. {
  1393. struct em28xx_fh *fh = filp->private_data;
  1394. struct em28xx *dev = fh->dev;
  1395. int errCode;
  1396. em28xx_videodbg("users=%d\n", dev->users);
  1397. mutex_lock(&dev->lock);
  1398. vb2_fop_release(filp);
  1399. if (dev->users == 1) {
  1400. /* the device is already disconnect,
  1401. free the remaining resources */
  1402. if (dev->disconnected) {
  1403. em28xx_release_resources(dev);
  1404. kfree(dev->alt_max_pkt_size_isoc);
  1405. mutex_unlock(&dev->lock);
  1406. kfree(dev);
  1407. return 0;
  1408. }
  1409. /* Save some power by putting tuner to sleep */
  1410. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1411. /* do this before setting alternate! */
  1412. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1413. /* set alternate 0 */
  1414. dev->alt = 0;
  1415. em28xx_videodbg("setting alternate 0\n");
  1416. errCode = usb_set_interface(dev->udev, 0, 0);
  1417. if (errCode < 0) {
  1418. em28xx_errdev("cannot change alternate number to "
  1419. "0 (error=%i)\n", errCode);
  1420. }
  1421. }
  1422. dev->users--;
  1423. mutex_unlock(&dev->lock);
  1424. return 0;
  1425. }
  1426. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1427. .owner = THIS_MODULE,
  1428. .open = em28xx_v4l2_open,
  1429. .release = em28xx_v4l2_close,
  1430. .read = vb2_fop_read,
  1431. .poll = vb2_fop_poll,
  1432. .mmap = vb2_fop_mmap,
  1433. .unlocked_ioctl = video_ioctl2,
  1434. };
  1435. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1436. .vidioc_querycap = vidioc_querycap,
  1437. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1438. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1439. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1440. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1441. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1442. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1443. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1444. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1445. .vidioc_g_audio = vidioc_g_audio,
  1446. .vidioc_s_audio = vidioc_s_audio,
  1447. .vidioc_cropcap = vidioc_cropcap,
  1448. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1449. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1450. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1451. .vidioc_querybuf = vb2_ioctl_querybuf,
  1452. .vidioc_qbuf = vb2_ioctl_qbuf,
  1453. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1454. .vidioc_g_std = vidioc_g_std,
  1455. .vidioc_querystd = vidioc_querystd,
  1456. .vidioc_s_std = vidioc_s_std,
  1457. .vidioc_g_parm = vidioc_g_parm,
  1458. .vidioc_s_parm = vidioc_s_parm,
  1459. .vidioc_enum_input = vidioc_enum_input,
  1460. .vidioc_g_input = vidioc_g_input,
  1461. .vidioc_s_input = vidioc_s_input,
  1462. .vidioc_streamon = vb2_ioctl_streamon,
  1463. .vidioc_streamoff = vb2_ioctl_streamoff,
  1464. .vidioc_g_tuner = vidioc_g_tuner,
  1465. .vidioc_s_tuner = vidioc_s_tuner,
  1466. .vidioc_g_frequency = vidioc_g_frequency,
  1467. .vidioc_s_frequency = vidioc_s_frequency,
  1468. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1469. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1470. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1471. .vidioc_g_register = vidioc_g_register,
  1472. .vidioc_s_register = vidioc_s_register,
  1473. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1474. #endif
  1475. };
  1476. static const struct video_device em28xx_video_template = {
  1477. .fops = &em28xx_v4l_fops,
  1478. .release = video_device_release_empty,
  1479. .ioctl_ops = &video_ioctl_ops,
  1480. .tvnorms = V4L2_STD_ALL,
  1481. };
  1482. static const struct v4l2_file_operations radio_fops = {
  1483. .owner = THIS_MODULE,
  1484. .open = em28xx_v4l2_open,
  1485. .release = em28xx_v4l2_close,
  1486. .unlocked_ioctl = video_ioctl2,
  1487. };
  1488. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1489. .vidioc_querycap = vidioc_querycap,
  1490. .vidioc_g_tuner = radio_g_tuner,
  1491. .vidioc_s_tuner = radio_s_tuner,
  1492. .vidioc_g_frequency = vidioc_g_frequency,
  1493. .vidioc_s_frequency = vidioc_s_frequency,
  1494. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1495. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1496. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1497. .vidioc_g_register = vidioc_g_register,
  1498. .vidioc_s_register = vidioc_s_register,
  1499. #endif
  1500. };
  1501. static struct video_device em28xx_radio_template = {
  1502. .name = "em28xx-radio",
  1503. .fops = &radio_fops,
  1504. .ioctl_ops = &radio_ioctl_ops,
  1505. };
  1506. /******************************** usb interface ******************************/
  1507. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1508. const struct video_device *template,
  1509. const char *type_name)
  1510. {
  1511. struct video_device *vfd;
  1512. vfd = video_device_alloc();
  1513. if (NULL == vfd)
  1514. return NULL;
  1515. *vfd = *template;
  1516. vfd->v4l2_dev = &dev->v4l2_dev;
  1517. vfd->debug = video_debug;
  1518. vfd->lock = &dev->lock;
  1519. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1520. if (dev->board.is_webcam)
  1521. vfd->tvnorms = 0;
  1522. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1523. dev->name, type_name);
  1524. video_set_drvdata(vfd, dev);
  1525. return vfd;
  1526. }
  1527. int em28xx_register_analog_devices(struct em28xx *dev)
  1528. {
  1529. u8 val;
  1530. int ret;
  1531. unsigned int maxw;
  1532. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  1533. dev->name, EM28XX_VERSION);
  1534. /* set default norm */
  1535. dev->norm = V4L2_STD_PAL;
  1536. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1537. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1538. /* Analog specific initialization */
  1539. dev->format = &format[0];
  1540. maxw = norm_maxw(dev);
  1541. /* MaxPacketSize for em2800 is too small to capture at full resolution
  1542. * use half of maxw as the scaler can only scale to 50% */
  1543. if (dev->board.is_em2800)
  1544. maxw /= 2;
  1545. em28xx_set_video_format(dev, format[0].fourcc,
  1546. maxw, norm_maxh(dev));
  1547. video_mux(dev, 0);
  1548. /* Audio defaults */
  1549. dev->mute = 1;
  1550. dev->volume = 0x1f;
  1551. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1552. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1553. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1554. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1555. em28xx_set_outfmt(dev);
  1556. em28xx_colorlevels_set_default(dev);
  1557. em28xx_compression_disable(dev);
  1558. /* allocate and fill video video_device struct */
  1559. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1560. if (!dev->vdev) {
  1561. em28xx_errdev("cannot allocate video_device.\n");
  1562. return -ENODEV;
  1563. }
  1564. dev->vdev->queue = &dev->vb_vidq;
  1565. dev->vdev->queue->lock = &dev->vb_queue_lock;
  1566. /* register v4l2 video video_device */
  1567. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1568. video_nr[dev->devno]);
  1569. if (ret) {
  1570. em28xx_errdev("unable to register video device (error=%i).\n",
  1571. ret);
  1572. return ret;
  1573. }
  1574. /* Allocate and fill vbi video_device struct */
  1575. if (em28xx_vbi_supported(dev) == 1) {
  1576. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1577. "vbi");
  1578. dev->vbi_dev->queue = &dev->vb_vbiq;
  1579. dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
  1580. /* register v4l2 vbi video_device */
  1581. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1582. vbi_nr[dev->devno]);
  1583. if (ret < 0) {
  1584. em28xx_errdev("unable to register vbi device\n");
  1585. return ret;
  1586. }
  1587. }
  1588. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1589. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1590. "radio");
  1591. if (!dev->radio_dev) {
  1592. em28xx_errdev("cannot allocate video_device.\n");
  1593. return -ENODEV;
  1594. }
  1595. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1596. radio_nr[dev->devno]);
  1597. if (ret < 0) {
  1598. em28xx_errdev("can't register radio device\n");
  1599. return ret;
  1600. }
  1601. em28xx_info("Registered radio device as %s\n",
  1602. video_device_node_name(dev->radio_dev));
  1603. }
  1604. em28xx_info("V4L2 video device registered as %s\n",
  1605. video_device_node_name(dev->vdev));
  1606. if (dev->vbi_dev)
  1607. em28xx_info("V4L2 VBI device registered as %s\n",
  1608. video_device_node_name(dev->vbi_dev));
  1609. return 0;
  1610. }