em28xx-video.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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 void get_scale(struct em28xx *dev,
  670. unsigned int width, unsigned int height,
  671. unsigned int *hscale, unsigned int *vscale)
  672. {
  673. unsigned int maxw = norm_maxw(dev);
  674. unsigned int maxh = norm_maxh(dev);
  675. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  676. if (*hscale >= 0x4000)
  677. *hscale = 0x3fff;
  678. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  679. if (*vscale >= 0x4000)
  680. *vscale = 0x3fff;
  681. }
  682. /* ------------------------------------------------------------------
  683. IOCTL vidioc handling
  684. ------------------------------------------------------------------*/
  685. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  686. struct v4l2_format *f)
  687. {
  688. struct em28xx_fh *fh = priv;
  689. struct em28xx *dev = fh->dev;
  690. f->fmt.pix.width = dev->width;
  691. f->fmt.pix.height = dev->height;
  692. f->fmt.pix.pixelformat = dev->format->fourcc;
  693. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  694. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  695. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  696. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  697. if (dev->progressive)
  698. f->fmt.pix.field = V4L2_FIELD_NONE;
  699. else
  700. f->fmt.pix.field = dev->interlaced ?
  701. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  702. return 0;
  703. }
  704. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  705. {
  706. unsigned int i;
  707. for (i = 0; i < ARRAY_SIZE(format); i++)
  708. if (format[i].fourcc == fourcc)
  709. return &format[i];
  710. return NULL;
  711. }
  712. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  713. struct v4l2_format *f)
  714. {
  715. struct em28xx_fh *fh = priv;
  716. struct em28xx *dev = fh->dev;
  717. unsigned int width = f->fmt.pix.width;
  718. unsigned int height = f->fmt.pix.height;
  719. unsigned int maxw = norm_maxw(dev);
  720. unsigned int maxh = norm_maxh(dev);
  721. unsigned int hscale, vscale;
  722. struct em28xx_fmt *fmt;
  723. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  724. if (!fmt) {
  725. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  726. f->fmt.pix.pixelformat);
  727. return -EINVAL;
  728. }
  729. if (dev->board.is_em2800) {
  730. /* the em2800 can only scale down to 50% */
  731. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  732. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  733. /*
  734. * MaxPacketSize for em2800 is too small to capture at full
  735. * resolution use half of maxw as the scaler can only scale
  736. * to 50%
  737. */
  738. if (width == maxw && height == maxh)
  739. width /= 2;
  740. } else {
  741. /* width must even because of the YUYV format
  742. height must be even because of interlacing */
  743. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  744. 1, 0);
  745. }
  746. get_scale(dev, width, height, &hscale, &vscale);
  747. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  748. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  749. f->fmt.pix.width = width;
  750. f->fmt.pix.height = height;
  751. f->fmt.pix.pixelformat = fmt->fourcc;
  752. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  753. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  754. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  755. if (dev->progressive)
  756. f->fmt.pix.field = V4L2_FIELD_NONE;
  757. else
  758. f->fmt.pix.field = dev->interlaced ?
  759. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  760. return 0;
  761. }
  762. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  763. unsigned width, unsigned height)
  764. {
  765. struct em28xx_fmt *fmt;
  766. fmt = format_by_fourcc(fourcc);
  767. if (!fmt)
  768. return -EINVAL;
  769. dev->format = fmt;
  770. dev->width = width;
  771. dev->height = height;
  772. /* set new image size */
  773. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  774. em28xx_resolution_set(dev);
  775. return 0;
  776. }
  777. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  778. struct v4l2_format *f)
  779. {
  780. struct em28xx *dev = video_drvdata(file);
  781. if (dev->streaming_users > 0)
  782. return -EBUSY;
  783. vidioc_try_fmt_vid_cap(file, priv, f);
  784. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  785. f->fmt.pix.width, f->fmt.pix.height);
  786. }
  787. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  788. {
  789. struct em28xx_fh *fh = priv;
  790. struct em28xx *dev = fh->dev;
  791. *norm = dev->norm;
  792. return 0;
  793. }
  794. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  795. {
  796. struct em28xx_fh *fh = priv;
  797. struct em28xx *dev = fh->dev;
  798. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  799. return 0;
  800. }
  801. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  802. {
  803. struct em28xx_fh *fh = priv;
  804. struct em28xx *dev = fh->dev;
  805. struct v4l2_format f;
  806. if (*norm == dev->norm)
  807. return 0;
  808. if (dev->streaming_users > 0)
  809. return -EBUSY;
  810. dev->norm = *norm;
  811. /* Adjusts width/height, if needed */
  812. f.fmt.pix.width = 720;
  813. f.fmt.pix.height = (*norm & V4L2_STD_525_60) ? 480 : 576;
  814. vidioc_try_fmt_vid_cap(file, priv, &f);
  815. /* set new image size */
  816. dev->width = f.fmt.pix.width;
  817. dev->height = f.fmt.pix.height;
  818. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  819. em28xx_resolution_set(dev);
  820. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  821. return 0;
  822. }
  823. static int vidioc_g_parm(struct file *file, void *priv,
  824. struct v4l2_streamparm *p)
  825. {
  826. struct em28xx_fh *fh = priv;
  827. struct em28xx *dev = fh->dev;
  828. int rc = 0;
  829. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  830. if (dev->board.is_webcam)
  831. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  832. video, g_parm, p);
  833. else
  834. v4l2_video_std_frame_period(dev->norm,
  835. &p->parm.capture.timeperframe);
  836. return rc;
  837. }
  838. static int vidioc_s_parm(struct file *file, void *priv,
  839. struct v4l2_streamparm *p)
  840. {
  841. struct em28xx_fh *fh = priv;
  842. struct em28xx *dev = fh->dev;
  843. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  844. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  845. }
  846. static const char *iname[] = {
  847. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  848. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  849. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  850. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  851. [EM28XX_VMUX_SVIDEO] = "S-Video",
  852. [EM28XX_VMUX_TELEVISION] = "Television",
  853. [EM28XX_VMUX_CABLE] = "Cable TV",
  854. [EM28XX_VMUX_DVB] = "DVB",
  855. [EM28XX_VMUX_DEBUG] = "for debug only",
  856. };
  857. static int vidioc_enum_input(struct file *file, void *priv,
  858. struct v4l2_input *i)
  859. {
  860. struct em28xx_fh *fh = priv;
  861. struct em28xx *dev = fh->dev;
  862. unsigned int n;
  863. n = i->index;
  864. if (n >= MAX_EM28XX_INPUT)
  865. return -EINVAL;
  866. if (0 == INPUT(n)->type)
  867. return -EINVAL;
  868. i->index = n;
  869. i->type = V4L2_INPUT_TYPE_CAMERA;
  870. strcpy(i->name, iname[INPUT(n)->type]);
  871. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  872. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  873. i->type = V4L2_INPUT_TYPE_TUNER;
  874. i->std = dev->vdev->tvnorms;
  875. /* webcams do not have the STD API */
  876. if (dev->board.is_webcam)
  877. i->capabilities = 0;
  878. return 0;
  879. }
  880. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  881. {
  882. struct em28xx_fh *fh = priv;
  883. struct em28xx *dev = fh->dev;
  884. *i = dev->ctl_input;
  885. return 0;
  886. }
  887. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  888. {
  889. struct em28xx_fh *fh = priv;
  890. struct em28xx *dev = fh->dev;
  891. if (i >= MAX_EM28XX_INPUT)
  892. return -EINVAL;
  893. if (0 == INPUT(i)->type)
  894. return -EINVAL;
  895. video_mux(dev, i);
  896. return 0;
  897. }
  898. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  899. {
  900. struct em28xx_fh *fh = priv;
  901. struct em28xx *dev = fh->dev;
  902. switch (a->index) {
  903. case EM28XX_AMUX_VIDEO:
  904. strcpy(a->name, "Television");
  905. break;
  906. case EM28XX_AMUX_LINE_IN:
  907. strcpy(a->name, "Line In");
  908. break;
  909. case EM28XX_AMUX_VIDEO2:
  910. strcpy(a->name, "Television alt");
  911. break;
  912. case EM28XX_AMUX_PHONE:
  913. strcpy(a->name, "Phone");
  914. break;
  915. case EM28XX_AMUX_MIC:
  916. strcpy(a->name, "Mic");
  917. break;
  918. case EM28XX_AMUX_CD:
  919. strcpy(a->name, "CD");
  920. break;
  921. case EM28XX_AMUX_AUX:
  922. strcpy(a->name, "Aux");
  923. break;
  924. case EM28XX_AMUX_PCM_OUT:
  925. strcpy(a->name, "PCM");
  926. break;
  927. default:
  928. return -EINVAL;
  929. }
  930. a->index = dev->ctl_ainput;
  931. a->capability = V4L2_AUDCAP_STEREO;
  932. return 0;
  933. }
  934. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  935. {
  936. struct em28xx_fh *fh = priv;
  937. struct em28xx *dev = fh->dev;
  938. if (a->index >= MAX_EM28XX_INPUT)
  939. return -EINVAL;
  940. if (0 == INPUT(a->index)->type)
  941. return -EINVAL;
  942. dev->ctl_ainput = INPUT(a->index)->amux;
  943. dev->ctl_aoutput = INPUT(a->index)->aout;
  944. if (!dev->ctl_aoutput)
  945. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  946. return 0;
  947. }
  948. static int vidioc_g_tuner(struct file *file, void *priv,
  949. struct v4l2_tuner *t)
  950. {
  951. struct em28xx_fh *fh = priv;
  952. struct em28xx *dev = fh->dev;
  953. if (0 != t->index)
  954. return -EINVAL;
  955. strcpy(t->name, "Tuner");
  956. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  957. return 0;
  958. }
  959. static int vidioc_s_tuner(struct file *file, void *priv,
  960. struct v4l2_tuner *t)
  961. {
  962. struct em28xx_fh *fh = priv;
  963. struct em28xx *dev = fh->dev;
  964. if (0 != t->index)
  965. return -EINVAL;
  966. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  967. return 0;
  968. }
  969. static int vidioc_g_frequency(struct file *file, void *priv,
  970. struct v4l2_frequency *f)
  971. {
  972. struct em28xx_fh *fh = priv;
  973. struct em28xx *dev = fh->dev;
  974. if (0 != f->tuner)
  975. return -EINVAL;
  976. f->frequency = dev->ctl_freq;
  977. return 0;
  978. }
  979. static int vidioc_s_frequency(struct file *file, void *priv,
  980. struct v4l2_frequency *f)
  981. {
  982. struct em28xx_fh *fh = priv;
  983. struct em28xx *dev = fh->dev;
  984. if (0 != f->tuner)
  985. return -EINVAL;
  986. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  987. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  988. dev->ctl_freq = f->frequency;
  989. return 0;
  990. }
  991. static int vidioc_g_chip_ident(struct file *file, void *priv,
  992. struct v4l2_dbg_chip_ident *chip)
  993. {
  994. struct em28xx_fh *fh = priv;
  995. struct em28xx *dev = fh->dev;
  996. chip->ident = V4L2_IDENT_NONE;
  997. chip->revision = 0;
  998. if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
  999. if (v4l2_chip_match_host(&chip->match))
  1000. chip->ident = V4L2_IDENT_NONE;
  1001. return 0;
  1002. }
  1003. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1004. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  1005. return -EINVAL;
  1006. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1007. return 0;
  1008. }
  1009. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1010. static int em28xx_reg_len(int reg)
  1011. {
  1012. switch (reg) {
  1013. case EM28XX_R40_AC97LSB:
  1014. case EM28XX_R30_HSCALELOW:
  1015. case EM28XX_R32_VSCALELOW:
  1016. return 2;
  1017. default:
  1018. return 1;
  1019. }
  1020. }
  1021. static int vidioc_g_register(struct file *file, void *priv,
  1022. struct v4l2_dbg_register *reg)
  1023. {
  1024. struct em28xx_fh *fh = priv;
  1025. struct em28xx *dev = fh->dev;
  1026. int ret;
  1027. switch (reg->match.type) {
  1028. case V4L2_CHIP_MATCH_AC97:
  1029. ret = em28xx_read_ac97(dev, reg->reg);
  1030. if (ret < 0)
  1031. return ret;
  1032. reg->val = ret;
  1033. reg->size = 1;
  1034. return 0;
  1035. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1036. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1037. return 0;
  1038. case V4L2_CHIP_MATCH_I2C_ADDR:
  1039. /* TODO: is this correct? */
  1040. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1041. return 0;
  1042. default:
  1043. if (!v4l2_chip_match_host(&reg->match))
  1044. return -EINVAL;
  1045. }
  1046. /* Match host */
  1047. reg->size = em28xx_reg_len(reg->reg);
  1048. if (reg->size == 1) {
  1049. ret = em28xx_read_reg(dev, reg->reg);
  1050. if (ret < 0)
  1051. return ret;
  1052. reg->val = ret;
  1053. } else {
  1054. __le16 val = 0;
  1055. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1056. reg->reg, (char *)&val, 2);
  1057. if (ret < 0)
  1058. return ret;
  1059. reg->val = le16_to_cpu(val);
  1060. }
  1061. return 0;
  1062. }
  1063. static int vidioc_s_register(struct file *file, void *priv,
  1064. struct v4l2_dbg_register *reg)
  1065. {
  1066. struct em28xx_fh *fh = priv;
  1067. struct em28xx *dev = fh->dev;
  1068. __le16 buf;
  1069. switch (reg->match.type) {
  1070. case V4L2_CHIP_MATCH_AC97:
  1071. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1072. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1073. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1074. return 0;
  1075. case V4L2_CHIP_MATCH_I2C_ADDR:
  1076. /* TODO: is this correct? */
  1077. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1078. return 0;
  1079. default:
  1080. if (!v4l2_chip_match_host(&reg->match))
  1081. return -EINVAL;
  1082. }
  1083. /* Match host */
  1084. buf = cpu_to_le16(reg->val);
  1085. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1086. em28xx_reg_len(reg->reg));
  1087. }
  1088. #endif
  1089. static int vidioc_querycap(struct file *file, void *priv,
  1090. struct v4l2_capability *cap)
  1091. {
  1092. struct video_device *vdev = video_devdata(file);
  1093. struct em28xx_fh *fh = priv;
  1094. struct em28xx *dev = fh->dev;
  1095. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1096. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1097. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1098. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1099. cap->device_caps = V4L2_CAP_READWRITE |
  1100. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1101. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1102. cap->device_caps = V4L2_CAP_RADIO;
  1103. else
  1104. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  1105. if (dev->audio_mode.has_audio)
  1106. cap->device_caps |= V4L2_CAP_AUDIO;
  1107. if (dev->tuner_type != TUNER_ABSENT)
  1108. cap->device_caps |= V4L2_CAP_TUNER;
  1109. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  1110. V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1111. if (dev->vbi_dev)
  1112. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1113. if (dev->radio_dev)
  1114. cap->capabilities |= V4L2_CAP_RADIO;
  1115. return 0;
  1116. }
  1117. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1118. struct v4l2_fmtdesc *f)
  1119. {
  1120. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1121. return -EINVAL;
  1122. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1123. f->pixelformat = format[f->index].fourcc;
  1124. return 0;
  1125. }
  1126. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1127. struct v4l2_frmsizeenum *fsize)
  1128. {
  1129. struct em28xx_fh *fh = priv;
  1130. struct em28xx *dev = fh->dev;
  1131. struct em28xx_fmt *fmt;
  1132. unsigned int maxw = norm_maxw(dev);
  1133. unsigned int maxh = norm_maxh(dev);
  1134. fmt = format_by_fourcc(fsize->pixel_format);
  1135. if (!fmt) {
  1136. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1137. fsize->pixel_format);
  1138. return -EINVAL;
  1139. }
  1140. if (dev->board.is_em2800) {
  1141. if (fsize->index > 1)
  1142. return -EINVAL;
  1143. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1144. fsize->discrete.width = maxw / (1 + fsize->index);
  1145. fsize->discrete.height = maxh / (1 + fsize->index);
  1146. return 0;
  1147. }
  1148. if (fsize->index != 0)
  1149. return -EINVAL;
  1150. /* Report a continuous range */
  1151. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1152. fsize->stepwise.min_width = 48;
  1153. fsize->stepwise.min_height = 32;
  1154. fsize->stepwise.max_width = maxw;
  1155. fsize->stepwise.max_height = maxh;
  1156. fsize->stepwise.step_width = 1;
  1157. fsize->stepwise.step_height = 1;
  1158. return 0;
  1159. }
  1160. /* RAW VBI ioctls */
  1161. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1162. struct v4l2_format *format)
  1163. {
  1164. struct em28xx_fh *fh = priv;
  1165. struct em28xx *dev = fh->dev;
  1166. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1167. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1168. format->fmt.vbi.offset = 0;
  1169. format->fmt.vbi.flags = 0;
  1170. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1171. format->fmt.vbi.count[0] = dev->vbi_height;
  1172. format->fmt.vbi.count[1] = dev->vbi_height;
  1173. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1174. /* Varies by video standard (NTSC, PAL, etc.) */
  1175. if (dev->norm & V4L2_STD_525_60) {
  1176. /* NTSC */
  1177. format->fmt.vbi.start[0] = 10;
  1178. format->fmt.vbi.start[1] = 273;
  1179. } else if (dev->norm & V4L2_STD_625_50) {
  1180. /* PAL */
  1181. format->fmt.vbi.start[0] = 6;
  1182. format->fmt.vbi.start[1] = 318;
  1183. }
  1184. return 0;
  1185. }
  1186. /* ----------------------------------------------------------- */
  1187. /* RADIO ESPECIFIC IOCTLS */
  1188. /* ----------------------------------------------------------- */
  1189. static int radio_g_tuner(struct file *file, void *priv,
  1190. struct v4l2_tuner *t)
  1191. {
  1192. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1193. if (unlikely(t->index > 0))
  1194. return -EINVAL;
  1195. strcpy(t->name, "Radio");
  1196. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1197. return 0;
  1198. }
  1199. static int radio_s_tuner(struct file *file, void *priv,
  1200. struct v4l2_tuner *t)
  1201. {
  1202. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1203. if (0 != t->index)
  1204. return -EINVAL;
  1205. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1206. return 0;
  1207. }
  1208. /*
  1209. * em28xx_v4l2_open()
  1210. * inits the device and starts isoc transfer
  1211. */
  1212. static int em28xx_v4l2_open(struct file *filp)
  1213. {
  1214. struct video_device *vdev = video_devdata(filp);
  1215. struct em28xx *dev = video_drvdata(filp);
  1216. enum v4l2_buf_type fh_type = 0;
  1217. struct em28xx_fh *fh;
  1218. switch (vdev->vfl_type) {
  1219. case VFL_TYPE_GRABBER:
  1220. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1221. break;
  1222. case VFL_TYPE_VBI:
  1223. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1224. break;
  1225. }
  1226. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1227. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1228. dev->users);
  1229. if (mutex_lock_interruptible(&dev->lock))
  1230. return -ERESTARTSYS;
  1231. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1232. if (!fh) {
  1233. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1234. mutex_unlock(&dev->lock);
  1235. return -ENOMEM;
  1236. }
  1237. v4l2_fh_init(&fh->fh, vdev);
  1238. fh->dev = dev;
  1239. fh->type = fh_type;
  1240. filp->private_data = fh;
  1241. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1242. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1243. em28xx_resolution_set(dev);
  1244. /* Needed, since GPIO might have disabled power of
  1245. some i2c device
  1246. */
  1247. em28xx_wake_i2c(dev);
  1248. }
  1249. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1250. em28xx_videodbg("video_open: setting radio device\n");
  1251. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1252. }
  1253. dev->users++;
  1254. mutex_unlock(&dev->lock);
  1255. v4l2_fh_add(&fh->fh);
  1256. return 0;
  1257. }
  1258. /*
  1259. * em28xx_realease_resources()
  1260. * unregisters the v4l2,i2c and usb devices
  1261. * called when the device gets disconected or at module unload
  1262. */
  1263. void em28xx_release_analog_resources(struct em28xx *dev)
  1264. {
  1265. /*FIXME: I2C IR should be disconnected */
  1266. if (dev->radio_dev) {
  1267. if (video_is_registered(dev->radio_dev))
  1268. video_unregister_device(dev->radio_dev);
  1269. else
  1270. video_device_release(dev->radio_dev);
  1271. dev->radio_dev = NULL;
  1272. }
  1273. if (dev->vbi_dev) {
  1274. em28xx_info("V4L2 device %s deregistered\n",
  1275. video_device_node_name(dev->vbi_dev));
  1276. if (video_is_registered(dev->vbi_dev))
  1277. video_unregister_device(dev->vbi_dev);
  1278. else
  1279. video_device_release(dev->vbi_dev);
  1280. dev->vbi_dev = NULL;
  1281. }
  1282. if (dev->vdev) {
  1283. em28xx_info("V4L2 device %s deregistered\n",
  1284. video_device_node_name(dev->vdev));
  1285. if (video_is_registered(dev->vdev))
  1286. video_unregister_device(dev->vdev);
  1287. else
  1288. video_device_release(dev->vdev);
  1289. dev->vdev = NULL;
  1290. }
  1291. }
  1292. /*
  1293. * em28xx_v4l2_close()
  1294. * stops streaming and deallocates all resources allocated by the v4l2
  1295. * calls and ioctls
  1296. */
  1297. static int em28xx_v4l2_close(struct file *filp)
  1298. {
  1299. struct em28xx_fh *fh = filp->private_data;
  1300. struct em28xx *dev = fh->dev;
  1301. int errCode;
  1302. em28xx_videodbg("users=%d\n", dev->users);
  1303. mutex_lock(&dev->lock);
  1304. vb2_fop_release(filp);
  1305. if (dev->users == 1) {
  1306. /* the device is already disconnect,
  1307. free the remaining resources */
  1308. if (dev->disconnected) {
  1309. em28xx_release_resources(dev);
  1310. kfree(dev->alt_max_pkt_size_isoc);
  1311. mutex_unlock(&dev->lock);
  1312. kfree(dev);
  1313. return 0;
  1314. }
  1315. /* Save some power by putting tuner to sleep */
  1316. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1317. /* do this before setting alternate! */
  1318. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1319. /* set alternate 0 */
  1320. dev->alt = 0;
  1321. em28xx_videodbg("setting alternate 0\n");
  1322. errCode = usb_set_interface(dev->udev, 0, 0);
  1323. if (errCode < 0) {
  1324. em28xx_errdev("cannot change alternate number to "
  1325. "0 (error=%i)\n", errCode);
  1326. }
  1327. }
  1328. dev->users--;
  1329. mutex_unlock(&dev->lock);
  1330. return 0;
  1331. }
  1332. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1333. .owner = THIS_MODULE,
  1334. .open = em28xx_v4l2_open,
  1335. .release = em28xx_v4l2_close,
  1336. .read = vb2_fop_read,
  1337. .poll = vb2_fop_poll,
  1338. .mmap = vb2_fop_mmap,
  1339. .unlocked_ioctl = video_ioctl2,
  1340. };
  1341. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1342. .vidioc_querycap = vidioc_querycap,
  1343. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1344. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1345. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1346. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1347. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1348. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1349. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1350. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1351. .vidioc_g_audio = vidioc_g_audio,
  1352. .vidioc_s_audio = vidioc_s_audio,
  1353. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1354. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1355. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1356. .vidioc_querybuf = vb2_ioctl_querybuf,
  1357. .vidioc_qbuf = vb2_ioctl_qbuf,
  1358. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1359. .vidioc_g_std = vidioc_g_std,
  1360. .vidioc_querystd = vidioc_querystd,
  1361. .vidioc_s_std = vidioc_s_std,
  1362. .vidioc_g_parm = vidioc_g_parm,
  1363. .vidioc_s_parm = vidioc_s_parm,
  1364. .vidioc_enum_input = vidioc_enum_input,
  1365. .vidioc_g_input = vidioc_g_input,
  1366. .vidioc_s_input = vidioc_s_input,
  1367. .vidioc_streamon = vb2_ioctl_streamon,
  1368. .vidioc_streamoff = vb2_ioctl_streamoff,
  1369. .vidioc_g_tuner = vidioc_g_tuner,
  1370. .vidioc_s_tuner = vidioc_s_tuner,
  1371. .vidioc_g_frequency = vidioc_g_frequency,
  1372. .vidioc_s_frequency = vidioc_s_frequency,
  1373. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1374. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1375. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1376. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1377. .vidioc_g_register = vidioc_g_register,
  1378. .vidioc_s_register = vidioc_s_register,
  1379. #endif
  1380. };
  1381. static const struct video_device em28xx_video_template = {
  1382. .fops = &em28xx_v4l_fops,
  1383. .release = video_device_release_empty,
  1384. .ioctl_ops = &video_ioctl_ops,
  1385. .tvnorms = V4L2_STD_ALL,
  1386. };
  1387. static const struct v4l2_file_operations radio_fops = {
  1388. .owner = THIS_MODULE,
  1389. .open = em28xx_v4l2_open,
  1390. .release = em28xx_v4l2_close,
  1391. .unlocked_ioctl = video_ioctl2,
  1392. };
  1393. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1394. .vidioc_querycap = vidioc_querycap,
  1395. .vidioc_g_tuner = radio_g_tuner,
  1396. .vidioc_s_tuner = radio_s_tuner,
  1397. .vidioc_g_frequency = vidioc_g_frequency,
  1398. .vidioc_s_frequency = vidioc_s_frequency,
  1399. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1400. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1401. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1402. .vidioc_g_register = vidioc_g_register,
  1403. .vidioc_s_register = vidioc_s_register,
  1404. #endif
  1405. };
  1406. static struct video_device em28xx_radio_template = {
  1407. .name = "em28xx-radio",
  1408. .fops = &radio_fops,
  1409. .ioctl_ops = &radio_ioctl_ops,
  1410. };
  1411. /******************************** usb interface ******************************/
  1412. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1413. const struct video_device *template,
  1414. const char *type_name)
  1415. {
  1416. struct video_device *vfd;
  1417. vfd = video_device_alloc();
  1418. if (NULL == vfd)
  1419. return NULL;
  1420. *vfd = *template;
  1421. vfd->v4l2_dev = &dev->v4l2_dev;
  1422. vfd->debug = video_debug;
  1423. vfd->lock = &dev->lock;
  1424. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1425. if (dev->board.is_webcam)
  1426. vfd->tvnorms = 0;
  1427. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1428. dev->name, type_name);
  1429. video_set_drvdata(vfd, dev);
  1430. return vfd;
  1431. }
  1432. int em28xx_register_analog_devices(struct em28xx *dev)
  1433. {
  1434. u8 val;
  1435. int ret;
  1436. unsigned int maxw;
  1437. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  1438. dev->name, EM28XX_VERSION);
  1439. /* set default norm */
  1440. dev->norm = V4L2_STD_PAL;
  1441. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1442. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1443. /* Analog specific initialization */
  1444. dev->format = &format[0];
  1445. maxw = norm_maxw(dev);
  1446. /* MaxPacketSize for em2800 is too small to capture at full resolution
  1447. * use half of maxw as the scaler can only scale to 50% */
  1448. if (dev->board.is_em2800)
  1449. maxw /= 2;
  1450. em28xx_set_video_format(dev, format[0].fourcc,
  1451. maxw, norm_maxh(dev));
  1452. video_mux(dev, 0);
  1453. /* Audio defaults */
  1454. dev->mute = 1;
  1455. dev->volume = 0x1f;
  1456. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1457. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1458. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1459. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1460. em28xx_set_outfmt(dev);
  1461. em28xx_colorlevels_set_default(dev);
  1462. em28xx_compression_disable(dev);
  1463. /* allocate and fill video video_device struct */
  1464. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1465. if (!dev->vdev) {
  1466. em28xx_errdev("cannot allocate video_device.\n");
  1467. return -ENODEV;
  1468. }
  1469. dev->vdev->queue = &dev->vb_vidq;
  1470. dev->vdev->queue->lock = &dev->vb_queue_lock;
  1471. /* disable inapplicable ioctls */
  1472. if (dev->board.is_webcam) {
  1473. v4l2_disable_ioctl(dev->vdev, VIDIOC_QUERYSTD);
  1474. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_STD);
  1475. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_STD);
  1476. } else {
  1477. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
  1478. }
  1479. if (dev->tuner_type == TUNER_ABSENT) {
  1480. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_TUNER);
  1481. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_TUNER);
  1482. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_FREQUENCY);
  1483. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_FREQUENCY);
  1484. }
  1485. if (!dev->audio_mode.has_audio) {
  1486. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_AUDIO);
  1487. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_AUDIO);
  1488. }
  1489. /* register v4l2 video video_device */
  1490. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1491. video_nr[dev->devno]);
  1492. if (ret) {
  1493. em28xx_errdev("unable to register video device (error=%i).\n",
  1494. ret);
  1495. return ret;
  1496. }
  1497. /* Allocate and fill vbi video_device struct */
  1498. if (em28xx_vbi_supported(dev) == 1) {
  1499. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1500. "vbi");
  1501. dev->vbi_dev->queue = &dev->vb_vbiq;
  1502. dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
  1503. /* disable inapplicable ioctls */
  1504. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
  1505. if (dev->tuner_type == TUNER_ABSENT) {
  1506. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_TUNER);
  1507. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_TUNER);
  1508. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_FREQUENCY);
  1509. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_FREQUENCY);
  1510. }
  1511. if (!dev->audio_mode.has_audio) {
  1512. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_AUDIO);
  1513. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_AUDIO);
  1514. }
  1515. /* register v4l2 vbi video_device */
  1516. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1517. vbi_nr[dev->devno]);
  1518. if (ret < 0) {
  1519. em28xx_errdev("unable to register vbi device\n");
  1520. return ret;
  1521. }
  1522. }
  1523. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1524. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1525. "radio");
  1526. if (!dev->radio_dev) {
  1527. em28xx_errdev("cannot allocate video_device.\n");
  1528. return -ENODEV;
  1529. }
  1530. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1531. radio_nr[dev->devno]);
  1532. if (ret < 0) {
  1533. em28xx_errdev("can't register radio device\n");
  1534. return ret;
  1535. }
  1536. em28xx_info("Registered radio device as %s\n",
  1537. video_device_node_name(dev->radio_dev));
  1538. }
  1539. em28xx_info("V4L2 video device registered as %s\n",
  1540. video_device_node_name(dev->vdev));
  1541. if (dev->vbi_dev)
  1542. em28xx_info("V4L2 VBI device registered as %s\n",
  1543. video_device_node_name(dev->vbi_dev));
  1544. return 0;
  1545. }