em28xx-video.c 52 KB

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