em28xx-video.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  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-chip-ident.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.1.3"
  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. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  65. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  66. module_param_array(video_nr, int, NULL, 0444);
  67. module_param_array(vbi_nr, int, NULL, 0444);
  68. module_param_array(radio_nr, int, NULL, 0444);
  69. MODULE_PARM_DESC(video_nr, "video device numbers");
  70. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  71. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  72. static unsigned int video_debug;
  73. module_param(video_debug, int, 0644);
  74. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  75. /* supported video standards */
  76. static struct em28xx_fmt format[] = {
  77. {
  78. .name = "16 bpp YUY2, 4:2:2, packed",
  79. .fourcc = V4L2_PIX_FMT_YUYV,
  80. .depth = 16,
  81. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  82. }, {
  83. .name = "16 bpp RGB 565, LE",
  84. .fourcc = V4L2_PIX_FMT_RGB565,
  85. .depth = 16,
  86. .reg = EM28XX_OUTFMT_RGB_16_656,
  87. }, {
  88. .name = "8 bpp Bayer BGBG..GRGR",
  89. .fourcc = V4L2_PIX_FMT_SBGGR8,
  90. .depth = 8,
  91. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  92. }, {
  93. .name = "8 bpp Bayer GRGR..BGBG",
  94. .fourcc = V4L2_PIX_FMT_SGRBG8,
  95. .depth = 8,
  96. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  97. }, {
  98. .name = "8 bpp Bayer GBGB..RGRG",
  99. .fourcc = V4L2_PIX_FMT_SGBRG8,
  100. .depth = 8,
  101. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  102. }, {
  103. .name = "12 bpp YUV411",
  104. .fourcc = V4L2_PIX_FMT_YUV411P,
  105. .depth = 12,
  106. .reg = EM28XX_OUTFMT_YUV411,
  107. },
  108. };
  109. /* ------------------------------------------------------------------
  110. DMA and thread functions
  111. ------------------------------------------------------------------*/
  112. /*
  113. * Finish the current buffer
  114. */
  115. static inline void finish_buffer(struct em28xx *dev,
  116. struct em28xx_buffer *buf)
  117. {
  118. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  119. buf->vb.state = VIDEOBUF_DONE;
  120. buf->vb.field_count++;
  121. v4l2_get_timestamp(&buf->vb.ts);
  122. list_del(&buf->vb.queue);
  123. wake_up(&buf->vb.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->vb.size)
  137. len = buf->vb.size - 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->vb.size) {
  154. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  155. ((char *)startwrite + lencopy) -
  156. ((char *)buf->vb_buf + buf->vb.size));
  157. remain = (char *)buf->vb_buf + buf->vb.size -
  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->vb.size) {
  177. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  178. "(2)\n",
  179. ((char *)startwrite + lencopy) -
  180. ((char *)buf->vb_buf + buf->vb.size));
  181. lencopy = remain = (char *)buf->vb_buf + buf->vb.size -
  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->vb.size)
  201. len = buf->vb.size - 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. char *outp;
  254. if (list_empty(&dma_q->active)) {
  255. em28xx_isocdbg("No active queue to serve\n");
  256. return NULL;
  257. }
  258. /* Get the next buffer */
  259. buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  260. /* Cleans up buffer - Useful for testing for frame/URB loss */
  261. outp = videobuf_to_vmalloc(&buf->vb);
  262. memset(outp, 0, buf->vb.size);
  263. buf->pos = 0;
  264. buf->vb_buf = outp;
  265. return buf;
  266. }
  267. /*
  268. * Finish the current buffer if completed and prepare for the next field
  269. */
  270. static struct em28xx_buffer *
  271. finish_field_prepare_next(struct em28xx *dev,
  272. struct em28xx_buffer *buf,
  273. struct em28xx_dmaqueue *dma_q)
  274. {
  275. if (dev->progressive || dev->top_field) { /* Brand new frame */
  276. if (buf != NULL)
  277. finish_buffer(dev, buf);
  278. buf = get_next_buf(dev, dma_q);
  279. }
  280. if (buf != NULL) {
  281. buf->top_field = dev->top_field;
  282. buf->pos = 0;
  283. }
  284. return buf;
  285. }
  286. /*
  287. * Process data packet according to the em2710/em2750/em28xx frame data format
  288. */
  289. static inline void process_frame_data_em28xx(struct em28xx *dev,
  290. unsigned char *data_pkt,
  291. unsigned int data_len)
  292. {
  293. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  294. struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
  295. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  296. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  297. /* capture type 0 = vbi start
  298. capture type 1 = vbi in progress
  299. capture type 2 = video start
  300. capture type 3 = video in progress */
  301. if (data_len >= 4) {
  302. /* NOTE: Headers are always 4 bytes and
  303. * never split across packets */
  304. if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
  305. data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
  306. /* Continuation */
  307. data_pkt += 4;
  308. data_len -= 4;
  309. } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
  310. /* Field start (VBI mode) */
  311. dev->capture_type = 0;
  312. dev->vbi_read = 0;
  313. em28xx_isocdbg("VBI START HEADER !!!\n");
  314. dev->top_field = !(data_pkt[2] & 1);
  315. data_pkt += 4;
  316. data_len -= 4;
  317. } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
  318. /* Field start (VBI disabled) */
  319. dev->capture_type = 2;
  320. em28xx_isocdbg("VIDEO START HEADER !!!\n");
  321. dev->top_field = !(data_pkt[2] & 1);
  322. data_pkt += 4;
  323. data_len -= 4;
  324. }
  325. }
  326. /* NOTE: With bulk transfers, intermediate data packets
  327. * have no continuation header */
  328. if (dev->capture_type == 0) {
  329. vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
  330. dev->usb_ctl.vbi_buf = vbi_buf;
  331. dev->capture_type = 1;
  332. }
  333. if (dev->capture_type == 1) {
  334. int vbi_size = dev->vbi_width * dev->vbi_height;
  335. int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
  336. (vbi_size - dev->vbi_read) : data_len;
  337. /* Copy VBI data */
  338. if (vbi_buf != NULL)
  339. em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
  340. dev->vbi_read += vbi_data_len;
  341. if (vbi_data_len < data_len) {
  342. /* Continue with copying video data */
  343. dev->capture_type = 2;
  344. data_pkt += vbi_data_len;
  345. data_len -= vbi_data_len;
  346. }
  347. }
  348. if (dev->capture_type == 2) {
  349. buf = finish_field_prepare_next(dev, buf, dma_q);
  350. dev->usb_ctl.vid_buf = buf;
  351. dev->capture_type = 3;
  352. }
  353. if (dev->capture_type == 3 && buf != NULL && data_len > 0)
  354. em28xx_copy_video(dev, buf, data_pkt, data_len);
  355. }
  356. /* Processes and copies the URB data content (video and VBI data) */
  357. static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
  358. {
  359. int xfer_bulk, num_packets, i;
  360. unsigned char *usb_data_pkt;
  361. unsigned int usb_data_len;
  362. if (!dev)
  363. return 0;
  364. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  365. return 0;
  366. if (urb->status < 0)
  367. print_err_status(dev, -1, urb->status);
  368. xfer_bulk = usb_pipebulk(urb->pipe);
  369. if (xfer_bulk) /* bulk */
  370. num_packets = 1;
  371. else /* isoc */
  372. num_packets = urb->number_of_packets;
  373. for (i = 0; i < num_packets; i++) {
  374. if (xfer_bulk) { /* bulk */
  375. usb_data_len = urb->actual_length;
  376. usb_data_pkt = urb->transfer_buffer;
  377. } else { /* isoc */
  378. if (urb->iso_frame_desc[i].status < 0) {
  379. print_err_status(dev, i,
  380. urb->iso_frame_desc[i].status);
  381. if (urb->iso_frame_desc[i].status != -EPROTO)
  382. continue;
  383. }
  384. usb_data_len = urb->iso_frame_desc[i].actual_length;
  385. if (usb_data_len > dev->max_pkt_size) {
  386. em28xx_isocdbg("packet bigger than packet size");
  387. continue;
  388. }
  389. usb_data_pkt = urb->transfer_buffer +
  390. urb->iso_frame_desc[i].offset;
  391. }
  392. if (usb_data_len == 0) {
  393. /* NOTE: happens very often with isoc transfers */
  394. /* em28xx_usbdbg("packet %d is empty",i); - spammy */
  395. continue;
  396. }
  397. process_frame_data_em28xx(dev, usb_data_pkt, usb_data_len);
  398. }
  399. return 1;
  400. }
  401. /* ------------------------------------------------------------------
  402. Videobuf operations
  403. ------------------------------------------------------------------*/
  404. static int
  405. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  406. {
  407. struct em28xx_fh *fh = vq->priv_data;
  408. struct em28xx *dev = fh->dev;
  409. struct v4l2_frequency f;
  410. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
  411. >> 3;
  412. if (0 == *count)
  413. *count = EM28XX_DEF_BUF;
  414. if (*count < EM28XX_MIN_BUF)
  415. *count = EM28XX_MIN_BUF;
  416. /* Ask tuner to go to analog or radio mode */
  417. memset(&f, 0, sizeof(f));
  418. f.frequency = dev->ctl_freq;
  419. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  420. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  421. return 0;
  422. }
  423. /* This is called *without* dev->slock held; please keep it that way */
  424. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  425. {
  426. struct em28xx_fh *fh = vq->priv_data;
  427. struct em28xx *dev = fh->dev;
  428. unsigned long flags = 0;
  429. if (in_interrupt())
  430. BUG();
  431. /* We used to wait for the buffer to finish here, but this didn't work
  432. because, as we were keeping the state as VIDEOBUF_QUEUED,
  433. videobuf_queue_cancel marked it as finished for us.
  434. (Also, it could wedge forever if the hardware was misconfigured.)
  435. This should be safe; by the time we get here, the buffer isn't
  436. queued anymore. If we ever start marking the buffers as
  437. VIDEOBUF_ACTIVE, it won't be, though.
  438. */
  439. spin_lock_irqsave(&dev->slock, flags);
  440. if (dev->usb_ctl.vid_buf == buf)
  441. dev->usb_ctl.vid_buf = NULL;
  442. spin_unlock_irqrestore(&dev->slock, flags);
  443. videobuf_vmalloc_free(&buf->vb);
  444. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  445. }
  446. static int
  447. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  448. enum v4l2_field field)
  449. {
  450. struct em28xx_fh *fh = vq->priv_data;
  451. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  452. struct em28xx *dev = fh->dev;
  453. int rc = 0, urb_init = 0;
  454. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  455. + 7) >> 3;
  456. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  457. return -EINVAL;
  458. buf->vb.width = dev->width;
  459. buf->vb.height = dev->height;
  460. buf->vb.field = field;
  461. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  462. rc = videobuf_iolock(vq, &buf->vb, NULL);
  463. if (rc < 0)
  464. goto fail;
  465. }
  466. if (!dev->usb_ctl.analog_bufs.num_bufs)
  467. urb_init = 1;
  468. if (urb_init) {
  469. dev->capture_type = -1;
  470. rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
  471. dev->analog_xfer_bulk,
  472. EM28XX_NUM_BUFS,
  473. dev->max_pkt_size,
  474. dev->packet_multiplier,
  475. em28xx_urb_data_copy);
  476. if (rc < 0)
  477. goto fail;
  478. }
  479. buf->vb.state = VIDEOBUF_PREPARED;
  480. return 0;
  481. fail:
  482. free_buffer(vq, buf);
  483. return rc;
  484. }
  485. static void
  486. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  487. {
  488. struct em28xx_buffer *buf = container_of(vb,
  489. struct em28xx_buffer,
  490. vb);
  491. struct em28xx_fh *fh = vq->priv_data;
  492. struct em28xx *dev = fh->dev;
  493. struct em28xx_dmaqueue *vidq = &dev->vidq;
  494. buf->vb.state = VIDEOBUF_QUEUED;
  495. list_add_tail(&buf->vb.queue, &vidq->active);
  496. }
  497. static void buffer_release(struct videobuf_queue *vq,
  498. struct videobuf_buffer *vb)
  499. {
  500. struct em28xx_buffer *buf = container_of(vb,
  501. struct em28xx_buffer,
  502. vb);
  503. struct em28xx_fh *fh = vq->priv_data;
  504. struct em28xx *dev = (struct em28xx *)fh->dev;
  505. em28xx_isocdbg("em28xx: called buffer_release\n");
  506. free_buffer(vq, buf);
  507. }
  508. static struct videobuf_queue_ops em28xx_video_qops = {
  509. .buf_setup = buffer_setup,
  510. .buf_prepare = buffer_prepare,
  511. .buf_queue = buffer_queue,
  512. .buf_release = buffer_release,
  513. };
  514. /********************* v4l2 interface **************************************/
  515. static void video_mux(struct em28xx *dev, int index)
  516. {
  517. dev->ctl_input = index;
  518. dev->ctl_ainput = INPUT(index)->amux;
  519. dev->ctl_aoutput = INPUT(index)->aout;
  520. if (!dev->ctl_aoutput)
  521. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  522. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  523. INPUT(index)->vmux, 0, 0);
  524. if (dev->board.has_msp34xx) {
  525. if (dev->i2s_speed) {
  526. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  527. s_i2s_clock_freq, dev->i2s_speed);
  528. }
  529. /* Note: this is msp3400 specific */
  530. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  531. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  532. }
  533. if (dev->board.adecoder != EM28XX_NOADECODER) {
  534. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  535. dev->ctl_ainput, dev->ctl_aoutput, 0);
  536. }
  537. em28xx_audio_analog_set(dev);
  538. }
  539. /* Usage lock check functions */
  540. static int res_get(struct em28xx_fh *fh, unsigned int bit)
  541. {
  542. struct em28xx *dev = fh->dev;
  543. if (fh->resources & bit)
  544. /* have it already allocated */
  545. return 1;
  546. /* is it free? */
  547. if (dev->resources & bit) {
  548. /* no, someone else uses it */
  549. return 0;
  550. }
  551. /* it's free, grab it */
  552. fh->resources |= bit;
  553. dev->resources |= bit;
  554. em28xx_videodbg("res: get %d\n", bit);
  555. return 1;
  556. }
  557. static int res_check(struct em28xx_fh *fh, unsigned int bit)
  558. {
  559. return fh->resources & bit;
  560. }
  561. static int res_locked(struct em28xx *dev, unsigned int bit)
  562. {
  563. return dev->resources & bit;
  564. }
  565. static void res_free(struct em28xx_fh *fh, unsigned int bits)
  566. {
  567. struct em28xx *dev = fh->dev;
  568. BUG_ON((fh->resources & bits) != bits);
  569. fh->resources &= ~bits;
  570. dev->resources &= ~bits;
  571. em28xx_videodbg("res: put %d\n", bits);
  572. }
  573. static int get_ressource(struct em28xx_fh *fh)
  574. {
  575. switch (fh->type) {
  576. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  577. return EM28XX_RESOURCE_VIDEO;
  578. case V4L2_BUF_TYPE_VBI_CAPTURE:
  579. return EM28XX_RESOURCE_VBI;
  580. default:
  581. BUG();
  582. return 0;
  583. }
  584. }
  585. void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
  586. {
  587. struct em28xx *dev = priv;
  588. /*
  589. * In the case of non-AC97 volume controls, we still need
  590. * to do some setups at em28xx, in order to mute/unmute
  591. * and to adjust audio volume. However, the value ranges
  592. * should be checked by the corresponding V4L subdriver.
  593. */
  594. switch (ctrl->id) {
  595. case V4L2_CID_AUDIO_MUTE:
  596. dev->mute = ctrl->val;
  597. em28xx_audio_analog_set(dev);
  598. break;
  599. case V4L2_CID_AUDIO_VOLUME:
  600. dev->volume = ctrl->val;
  601. em28xx_audio_analog_set(dev);
  602. break;
  603. }
  604. }
  605. static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
  606. {
  607. struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
  608. switch (ctrl->id) {
  609. case V4L2_CID_AUDIO_MUTE:
  610. dev->mute = ctrl->val;
  611. break;
  612. case V4L2_CID_AUDIO_VOLUME:
  613. dev->volume = ctrl->val;
  614. break;
  615. }
  616. return em28xx_audio_analog_set(dev);
  617. }
  618. const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
  619. .s_ctrl = em28xx_s_ctrl,
  620. };
  621. static int check_dev(struct em28xx *dev)
  622. {
  623. if (dev->state & DEV_DISCONNECTED) {
  624. em28xx_errdev("v4l2 ioctl: device not present\n");
  625. return -ENODEV;
  626. }
  627. if (dev->state & DEV_MISCONFIGURED) {
  628. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  629. "close and open it again\n");
  630. return -EIO;
  631. }
  632. return 0;
  633. }
  634. static void get_scale(struct em28xx *dev,
  635. unsigned int width, unsigned int height,
  636. unsigned int *hscale, unsigned int *vscale)
  637. {
  638. unsigned int maxw = norm_maxw(dev);
  639. unsigned int maxh = norm_maxh(dev);
  640. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  641. if (*hscale >= 0x4000)
  642. *hscale = 0x3fff;
  643. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  644. if (*vscale >= 0x4000)
  645. *vscale = 0x3fff;
  646. }
  647. /* ------------------------------------------------------------------
  648. IOCTL vidioc handling
  649. ------------------------------------------------------------------*/
  650. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  651. struct v4l2_format *f)
  652. {
  653. struct em28xx_fh *fh = priv;
  654. struct em28xx *dev = fh->dev;
  655. f->fmt.pix.width = dev->width;
  656. f->fmt.pix.height = dev->height;
  657. f->fmt.pix.pixelformat = dev->format->fourcc;
  658. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  659. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  660. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  661. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  662. if (dev->progressive)
  663. f->fmt.pix.field = V4L2_FIELD_NONE;
  664. else
  665. f->fmt.pix.field = dev->interlaced ?
  666. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  667. return 0;
  668. }
  669. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  670. {
  671. unsigned int i;
  672. for (i = 0; i < ARRAY_SIZE(format); i++)
  673. if (format[i].fourcc == fourcc)
  674. return &format[i];
  675. return NULL;
  676. }
  677. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  678. struct v4l2_format *f)
  679. {
  680. struct em28xx_fh *fh = priv;
  681. struct em28xx *dev = fh->dev;
  682. unsigned int width = f->fmt.pix.width;
  683. unsigned int height = f->fmt.pix.height;
  684. unsigned int maxw = norm_maxw(dev);
  685. unsigned int maxh = norm_maxh(dev);
  686. unsigned int hscale, vscale;
  687. struct em28xx_fmt *fmt;
  688. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  689. if (!fmt) {
  690. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  691. f->fmt.pix.pixelformat);
  692. return -EINVAL;
  693. }
  694. if (dev->board.is_em2800) {
  695. /* the em2800 can only scale down to 50% */
  696. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  697. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  698. /* MaxPacketSize for em2800 is too small to capture at full resolution
  699. * use half of maxw as the scaler can only scale to 50% */
  700. if (width == maxw && height == maxh)
  701. width /= 2;
  702. } else {
  703. /* width must even because of the YUYV format
  704. height must be even because of interlacing */
  705. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  706. 1, 0);
  707. }
  708. get_scale(dev, width, height, &hscale, &vscale);
  709. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  710. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  711. f->fmt.pix.width = width;
  712. f->fmt.pix.height = height;
  713. f->fmt.pix.pixelformat = fmt->fourcc;
  714. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  715. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  716. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  717. if (dev->progressive)
  718. f->fmt.pix.field = V4L2_FIELD_NONE;
  719. else
  720. f->fmt.pix.field = dev->interlaced ?
  721. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  722. return 0;
  723. }
  724. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  725. unsigned width, unsigned height)
  726. {
  727. struct em28xx_fmt *fmt;
  728. fmt = format_by_fourcc(fourcc);
  729. if (!fmt)
  730. return -EINVAL;
  731. dev->format = fmt;
  732. dev->width = width;
  733. dev->height = height;
  734. /* set new image size */
  735. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  736. em28xx_set_alternate(dev);
  737. em28xx_resolution_set(dev);
  738. return 0;
  739. }
  740. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  741. struct v4l2_format *f)
  742. {
  743. struct em28xx_fh *fh = priv;
  744. struct em28xx *dev = fh->dev;
  745. int rc;
  746. rc = check_dev(dev);
  747. if (rc < 0)
  748. return rc;
  749. vidioc_try_fmt_vid_cap(file, priv, f);
  750. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  751. em28xx_errdev("%s queue busy\n", __func__);
  752. return -EBUSY;
  753. }
  754. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  755. f->fmt.pix.width, f->fmt.pix.height);
  756. }
  757. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  758. {
  759. struct em28xx_fh *fh = priv;
  760. struct em28xx *dev = fh->dev;
  761. int rc;
  762. rc = check_dev(dev);
  763. if (rc < 0)
  764. return rc;
  765. *norm = dev->norm;
  766. return 0;
  767. }
  768. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  769. {
  770. struct em28xx_fh *fh = priv;
  771. struct em28xx *dev = fh->dev;
  772. int rc;
  773. rc = check_dev(dev);
  774. if (rc < 0)
  775. return rc;
  776. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  777. return 0;
  778. }
  779. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  780. {
  781. struct em28xx_fh *fh = priv;
  782. struct em28xx *dev = fh->dev;
  783. struct v4l2_format f;
  784. int rc;
  785. rc = check_dev(dev);
  786. if (rc < 0)
  787. return rc;
  788. dev->norm = *norm;
  789. /* Adjusts width/height, if needed */
  790. f.fmt.pix.width = dev->width;
  791. f.fmt.pix.height = dev->height;
  792. vidioc_try_fmt_vid_cap(file, priv, &f);
  793. /* set new image size */
  794. dev->width = f.fmt.pix.width;
  795. dev->height = f.fmt.pix.height;
  796. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  797. em28xx_resolution_set(dev);
  798. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  799. return 0;
  800. }
  801. static int vidioc_g_parm(struct file *file, void *priv,
  802. struct v4l2_streamparm *p)
  803. {
  804. struct em28xx_fh *fh = priv;
  805. struct em28xx *dev = fh->dev;
  806. int rc = 0;
  807. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  808. return -EINVAL;
  809. if (dev->board.is_webcam)
  810. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  811. video, g_parm, p);
  812. else
  813. v4l2_video_std_frame_period(dev->norm,
  814. &p->parm.capture.timeperframe);
  815. return rc;
  816. }
  817. static int vidioc_s_parm(struct file *file, void *priv,
  818. struct v4l2_streamparm *p)
  819. {
  820. struct em28xx_fh *fh = priv;
  821. struct em28xx *dev = fh->dev;
  822. if (!dev->board.is_webcam)
  823. return -EINVAL;
  824. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  825. return -EINVAL;
  826. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  827. }
  828. static const char *iname[] = {
  829. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  830. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  831. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  832. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  833. [EM28XX_VMUX_SVIDEO] = "S-Video",
  834. [EM28XX_VMUX_TELEVISION] = "Television",
  835. [EM28XX_VMUX_CABLE] = "Cable TV",
  836. [EM28XX_VMUX_DVB] = "DVB",
  837. [EM28XX_VMUX_DEBUG] = "for debug only",
  838. };
  839. static int vidioc_enum_input(struct file *file, void *priv,
  840. struct v4l2_input *i)
  841. {
  842. struct em28xx_fh *fh = priv;
  843. struct em28xx *dev = fh->dev;
  844. unsigned int n;
  845. n = i->index;
  846. if (n >= MAX_EM28XX_INPUT)
  847. return -EINVAL;
  848. if (0 == INPUT(n)->type)
  849. return -EINVAL;
  850. i->index = n;
  851. i->type = V4L2_INPUT_TYPE_CAMERA;
  852. strcpy(i->name, iname[INPUT(n)->type]);
  853. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  854. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  855. i->type = V4L2_INPUT_TYPE_TUNER;
  856. i->std = dev->vdev->tvnorms;
  857. return 0;
  858. }
  859. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  860. {
  861. struct em28xx_fh *fh = priv;
  862. struct em28xx *dev = fh->dev;
  863. *i = dev->ctl_input;
  864. return 0;
  865. }
  866. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  867. {
  868. struct em28xx_fh *fh = priv;
  869. struct em28xx *dev = fh->dev;
  870. int rc;
  871. rc = check_dev(dev);
  872. if (rc < 0)
  873. return rc;
  874. if (i >= MAX_EM28XX_INPUT)
  875. return -EINVAL;
  876. if (0 == INPUT(i)->type)
  877. return -EINVAL;
  878. video_mux(dev, i);
  879. return 0;
  880. }
  881. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  882. {
  883. struct em28xx_fh *fh = priv;
  884. struct em28xx *dev = fh->dev;
  885. if (!dev->audio_mode.has_audio)
  886. return -EINVAL;
  887. switch (a->index) {
  888. case EM28XX_AMUX_VIDEO:
  889. strcpy(a->name, "Television");
  890. break;
  891. case EM28XX_AMUX_LINE_IN:
  892. strcpy(a->name, "Line In");
  893. break;
  894. case EM28XX_AMUX_VIDEO2:
  895. strcpy(a->name, "Television alt");
  896. break;
  897. case EM28XX_AMUX_PHONE:
  898. strcpy(a->name, "Phone");
  899. break;
  900. case EM28XX_AMUX_MIC:
  901. strcpy(a->name, "Mic");
  902. break;
  903. case EM28XX_AMUX_CD:
  904. strcpy(a->name, "CD");
  905. break;
  906. case EM28XX_AMUX_AUX:
  907. strcpy(a->name, "Aux");
  908. break;
  909. case EM28XX_AMUX_PCM_OUT:
  910. strcpy(a->name, "PCM");
  911. break;
  912. default:
  913. return -EINVAL;
  914. }
  915. a->index = dev->ctl_ainput;
  916. a->capability = V4L2_AUDCAP_STEREO;
  917. return 0;
  918. }
  919. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  920. {
  921. struct em28xx_fh *fh = priv;
  922. struct em28xx *dev = fh->dev;
  923. if (!dev->audio_mode.has_audio)
  924. return -EINVAL;
  925. if (a->index >= MAX_EM28XX_INPUT)
  926. return -EINVAL;
  927. if (0 == INPUT(a->index)->type)
  928. return -EINVAL;
  929. dev->ctl_ainput = INPUT(a->index)->amux;
  930. dev->ctl_aoutput = INPUT(a->index)->aout;
  931. if (!dev->ctl_aoutput)
  932. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  933. return 0;
  934. }
  935. static int vidioc_g_tuner(struct file *file, void *priv,
  936. struct v4l2_tuner *t)
  937. {
  938. struct em28xx_fh *fh = priv;
  939. struct em28xx *dev = fh->dev;
  940. int rc;
  941. rc = check_dev(dev);
  942. if (rc < 0)
  943. return rc;
  944. if (0 != t->index)
  945. return -EINVAL;
  946. strcpy(t->name, "Tuner");
  947. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  948. return 0;
  949. }
  950. static int vidioc_s_tuner(struct file *file, void *priv,
  951. struct v4l2_tuner *t)
  952. {
  953. struct em28xx_fh *fh = priv;
  954. struct em28xx *dev = fh->dev;
  955. int rc;
  956. rc = check_dev(dev);
  957. if (rc < 0)
  958. return rc;
  959. if (0 != t->index)
  960. return -EINVAL;
  961. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  962. return 0;
  963. }
  964. static int vidioc_g_frequency(struct file *file, void *priv,
  965. struct v4l2_frequency *f)
  966. {
  967. struct em28xx_fh *fh = priv;
  968. struct em28xx *dev = fh->dev;
  969. if (0 != f->tuner)
  970. return -EINVAL;
  971. f->frequency = dev->ctl_freq;
  972. return 0;
  973. }
  974. static int vidioc_s_frequency(struct file *file, void *priv,
  975. struct v4l2_frequency *f)
  976. {
  977. struct em28xx_fh *fh = priv;
  978. struct em28xx *dev = fh->dev;
  979. int rc;
  980. rc = check_dev(dev);
  981. if (rc < 0)
  982. return rc;
  983. if (0 != f->tuner)
  984. return -EINVAL;
  985. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  986. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f);
  987. dev->ctl_freq = f->frequency;
  988. return 0;
  989. }
  990. #ifdef CONFIG_VIDEO_ADV_DEBUG
  991. static int em28xx_reg_len(int reg)
  992. {
  993. switch (reg) {
  994. case EM28XX_R40_AC97LSB:
  995. case EM28XX_R30_HSCALELOW:
  996. case EM28XX_R32_VSCALELOW:
  997. return 2;
  998. default:
  999. return 1;
  1000. }
  1001. }
  1002. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1003. struct v4l2_dbg_chip_ident *chip)
  1004. {
  1005. struct em28xx_fh *fh = priv;
  1006. struct em28xx *dev = fh->dev;
  1007. chip->ident = V4L2_IDENT_NONE;
  1008. chip->revision = 0;
  1009. if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
  1010. if (v4l2_chip_match_host(&chip->match))
  1011. chip->ident = V4L2_IDENT_NONE;
  1012. return 0;
  1013. }
  1014. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1015. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  1016. return -EINVAL;
  1017. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1018. return 0;
  1019. }
  1020. static int vidioc_g_register(struct file *file, void *priv,
  1021. struct v4l2_dbg_register *reg)
  1022. {
  1023. struct em28xx_fh *fh = priv;
  1024. struct em28xx *dev = fh->dev;
  1025. int ret;
  1026. switch (reg->match.type) {
  1027. case V4L2_CHIP_MATCH_AC97:
  1028. ret = em28xx_read_ac97(dev, reg->reg);
  1029. if (ret < 0)
  1030. return ret;
  1031. reg->val = ret;
  1032. reg->size = 1;
  1033. return 0;
  1034. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1035. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1036. return 0;
  1037. case V4L2_CHIP_MATCH_I2C_ADDR:
  1038. /* TODO: is this correct? */
  1039. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1040. return 0;
  1041. default:
  1042. if (!v4l2_chip_match_host(&reg->match))
  1043. return -EINVAL;
  1044. }
  1045. /* Match host */
  1046. reg->size = em28xx_reg_len(reg->reg);
  1047. if (reg->size == 1) {
  1048. ret = em28xx_read_reg(dev, reg->reg);
  1049. if (ret < 0)
  1050. return ret;
  1051. reg->val = ret;
  1052. } else {
  1053. __le16 val = 0;
  1054. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1055. reg->reg, (char *)&val, 2);
  1056. if (ret < 0)
  1057. return ret;
  1058. reg->val = le16_to_cpu(val);
  1059. }
  1060. return 0;
  1061. }
  1062. static int vidioc_s_register(struct file *file, void *priv,
  1063. struct v4l2_dbg_register *reg)
  1064. {
  1065. struct em28xx_fh *fh = priv;
  1066. struct em28xx *dev = fh->dev;
  1067. __le16 buf;
  1068. switch (reg->match.type) {
  1069. case V4L2_CHIP_MATCH_AC97:
  1070. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1071. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1072. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1073. return 0;
  1074. case V4L2_CHIP_MATCH_I2C_ADDR:
  1075. /* TODO: is this correct? */
  1076. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1077. return 0;
  1078. default:
  1079. if (!v4l2_chip_match_host(&reg->match))
  1080. return -EINVAL;
  1081. }
  1082. /* Match host */
  1083. buf = cpu_to_le16(reg->val);
  1084. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1085. em28xx_reg_len(reg->reg));
  1086. }
  1087. #endif
  1088. static int vidioc_cropcap(struct file *file, void *priv,
  1089. struct v4l2_cropcap *cc)
  1090. {
  1091. struct em28xx_fh *fh = priv;
  1092. struct em28xx *dev = fh->dev;
  1093. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1094. return -EINVAL;
  1095. cc->bounds.left = 0;
  1096. cc->bounds.top = 0;
  1097. cc->bounds.width = dev->width;
  1098. cc->bounds.height = dev->height;
  1099. cc->defrect = cc->bounds;
  1100. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1101. cc->pixelaspect.denominator = 59;
  1102. return 0;
  1103. }
  1104. static int vidioc_streamon(struct file *file, void *priv,
  1105. enum v4l2_buf_type type)
  1106. {
  1107. struct em28xx_fh *fh = priv;
  1108. struct em28xx *dev = fh->dev;
  1109. int rc = -EINVAL;
  1110. rc = check_dev(dev);
  1111. if (rc < 0)
  1112. return rc;
  1113. if (unlikely(type != fh->type))
  1114. return -EINVAL;
  1115. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1116. fh, type, fh->resources, dev->resources);
  1117. if (unlikely(!res_get(fh, get_ressource(fh))))
  1118. return -EBUSY;
  1119. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1120. rc = videobuf_streamon(&fh->vb_vidq);
  1121. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1122. rc = videobuf_streamon(&fh->vb_vbiq);
  1123. return rc;
  1124. }
  1125. static int vidioc_streamoff(struct file *file, void *priv,
  1126. enum v4l2_buf_type type)
  1127. {
  1128. struct em28xx_fh *fh = priv;
  1129. struct em28xx *dev = fh->dev;
  1130. int rc;
  1131. rc = check_dev(dev);
  1132. if (rc < 0)
  1133. return rc;
  1134. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1135. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1136. return -EINVAL;
  1137. if (type != fh->type)
  1138. return -EINVAL;
  1139. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1140. fh, type, fh->resources, dev->resources);
  1141. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1142. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1143. videobuf_streamoff(&fh->vb_vidq);
  1144. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1145. }
  1146. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1147. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1148. videobuf_streamoff(&fh->vb_vbiq);
  1149. res_free(fh, EM28XX_RESOURCE_VBI);
  1150. }
  1151. }
  1152. return 0;
  1153. }
  1154. static int vidioc_querycap(struct file *file, void *priv,
  1155. struct v4l2_capability *cap)
  1156. {
  1157. struct video_device *vdev = video_devdata(file);
  1158. struct em28xx_fh *fh = priv;
  1159. struct em28xx *dev = fh->dev;
  1160. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1161. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1162. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1163. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1164. cap->device_caps = V4L2_CAP_READWRITE |
  1165. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1166. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1167. cap->device_caps = V4L2_CAP_RADIO;
  1168. else
  1169. cap->device_caps = V4L2_CAP_READWRITE |
  1170. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
  1171. if (dev->audio_mode.has_audio)
  1172. cap->device_caps |= V4L2_CAP_AUDIO;
  1173. if (dev->tuner_type != TUNER_ABSENT)
  1174. cap->device_caps |= V4L2_CAP_TUNER;
  1175. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  1176. V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1177. if (dev->vbi_dev)
  1178. cap->capabilities |=
  1179. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_CAPTURE;
  1180. if (dev->radio_dev)
  1181. cap->capabilities |= V4L2_CAP_RADIO;
  1182. return 0;
  1183. }
  1184. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1185. struct v4l2_fmtdesc *f)
  1186. {
  1187. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1188. return -EINVAL;
  1189. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1190. f->pixelformat = format[f->index].fourcc;
  1191. return 0;
  1192. }
  1193. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1194. struct v4l2_frmsizeenum *fsize)
  1195. {
  1196. struct em28xx_fh *fh = priv;
  1197. struct em28xx *dev = fh->dev;
  1198. struct em28xx_fmt *fmt;
  1199. unsigned int maxw = norm_maxw(dev);
  1200. unsigned int maxh = norm_maxh(dev);
  1201. fmt = format_by_fourcc(fsize->pixel_format);
  1202. if (!fmt) {
  1203. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1204. fsize->pixel_format);
  1205. return -EINVAL;
  1206. }
  1207. if (dev->board.is_em2800) {
  1208. if (fsize->index > 1)
  1209. return -EINVAL;
  1210. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1211. fsize->discrete.width = maxw / (1 + fsize->index);
  1212. fsize->discrete.height = maxh / (1 + fsize->index);
  1213. return 0;
  1214. }
  1215. if (fsize->index != 0)
  1216. return -EINVAL;
  1217. /* Report a continuous range */
  1218. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1219. fsize->stepwise.min_width = 48;
  1220. fsize->stepwise.min_height = 32;
  1221. fsize->stepwise.max_width = maxw;
  1222. fsize->stepwise.max_height = maxh;
  1223. fsize->stepwise.step_width = 1;
  1224. fsize->stepwise.step_height = 1;
  1225. return 0;
  1226. }
  1227. /* Sliced VBI ioctls */
  1228. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1229. struct v4l2_format *f)
  1230. {
  1231. struct em28xx_fh *fh = priv;
  1232. struct em28xx *dev = fh->dev;
  1233. int rc;
  1234. rc = check_dev(dev);
  1235. if (rc < 0)
  1236. return rc;
  1237. f->fmt.sliced.service_set = 0;
  1238. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1239. if (f->fmt.sliced.service_set == 0)
  1240. rc = -EINVAL;
  1241. return rc;
  1242. }
  1243. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1244. struct v4l2_format *f)
  1245. {
  1246. struct em28xx_fh *fh = priv;
  1247. struct em28xx *dev = fh->dev;
  1248. int rc;
  1249. rc = check_dev(dev);
  1250. if (rc < 0)
  1251. return rc;
  1252. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1253. if (f->fmt.sliced.service_set == 0)
  1254. return -EINVAL;
  1255. return 0;
  1256. }
  1257. /* RAW VBI ioctls */
  1258. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1259. struct v4l2_format *format)
  1260. {
  1261. struct em28xx_fh *fh = priv;
  1262. struct em28xx *dev = fh->dev;
  1263. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1264. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1265. format->fmt.vbi.offset = 0;
  1266. format->fmt.vbi.flags = 0;
  1267. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1268. format->fmt.vbi.count[0] = dev->vbi_height;
  1269. format->fmt.vbi.count[1] = dev->vbi_height;
  1270. /* Varies by video standard (NTSC, PAL, etc.) */
  1271. if (dev->norm & V4L2_STD_525_60) {
  1272. /* NTSC */
  1273. format->fmt.vbi.start[0] = 10;
  1274. format->fmt.vbi.start[1] = 273;
  1275. } else if (dev->norm & V4L2_STD_625_50) {
  1276. /* PAL */
  1277. format->fmt.vbi.start[0] = 6;
  1278. format->fmt.vbi.start[1] = 318;
  1279. }
  1280. return 0;
  1281. }
  1282. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1283. struct v4l2_format *format)
  1284. {
  1285. struct em28xx_fh *fh = priv;
  1286. struct em28xx *dev = fh->dev;
  1287. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1288. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1289. format->fmt.vbi.offset = 0;
  1290. format->fmt.vbi.flags = 0;
  1291. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1292. format->fmt.vbi.count[0] = dev->vbi_height;
  1293. format->fmt.vbi.count[1] = dev->vbi_height;
  1294. /* Varies by video standard (NTSC, PAL, etc.) */
  1295. if (dev->norm & V4L2_STD_525_60) {
  1296. /* NTSC */
  1297. format->fmt.vbi.start[0] = 10;
  1298. format->fmt.vbi.start[1] = 273;
  1299. } else if (dev->norm & V4L2_STD_625_50) {
  1300. /* PAL */
  1301. format->fmt.vbi.start[0] = 6;
  1302. format->fmt.vbi.start[1] = 318;
  1303. }
  1304. return 0;
  1305. }
  1306. static int vidioc_reqbufs(struct file *file, void *priv,
  1307. struct v4l2_requestbuffers *rb)
  1308. {
  1309. struct em28xx_fh *fh = priv;
  1310. struct em28xx *dev = fh->dev;
  1311. int rc;
  1312. rc = check_dev(dev);
  1313. if (rc < 0)
  1314. return rc;
  1315. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1316. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1317. else
  1318. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1319. }
  1320. static int vidioc_querybuf(struct file *file, void *priv,
  1321. struct v4l2_buffer *b)
  1322. {
  1323. struct em28xx_fh *fh = priv;
  1324. struct em28xx *dev = fh->dev;
  1325. int rc;
  1326. rc = check_dev(dev);
  1327. if (rc < 0)
  1328. return rc;
  1329. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1330. return videobuf_querybuf(&fh->vb_vidq, b);
  1331. else {
  1332. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1333. the videobuf framework, but we probably shouldn't be
  1334. returning a buffer larger than that which was asked for.
  1335. At a minimum, it causes a crash in zvbi since it does
  1336. a memcpy based on the source buffer length */
  1337. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1338. b->length = dev->vbi_width * dev->vbi_height * 2;
  1339. return result;
  1340. }
  1341. }
  1342. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1343. {
  1344. struct em28xx_fh *fh = priv;
  1345. struct em28xx *dev = fh->dev;
  1346. int rc;
  1347. rc = check_dev(dev);
  1348. if (rc < 0)
  1349. return rc;
  1350. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1351. return videobuf_qbuf(&fh->vb_vidq, b);
  1352. else
  1353. return videobuf_qbuf(&fh->vb_vbiq, b);
  1354. }
  1355. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1356. {
  1357. struct em28xx_fh *fh = priv;
  1358. struct em28xx *dev = fh->dev;
  1359. int rc;
  1360. rc = check_dev(dev);
  1361. if (rc < 0)
  1362. return rc;
  1363. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1364. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1365. O_NONBLOCK);
  1366. else
  1367. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1368. O_NONBLOCK);
  1369. }
  1370. /* ----------------------------------------------------------- */
  1371. /* RADIO ESPECIFIC IOCTLS */
  1372. /* ----------------------------------------------------------- */
  1373. static int radio_g_tuner(struct file *file, void *priv,
  1374. struct v4l2_tuner *t)
  1375. {
  1376. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1377. if (unlikely(t->index > 0))
  1378. return -EINVAL;
  1379. strcpy(t->name, "Radio");
  1380. t->type = V4L2_TUNER_RADIO;
  1381. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1382. return 0;
  1383. }
  1384. static int radio_s_tuner(struct file *file, void *priv,
  1385. struct v4l2_tuner *t)
  1386. {
  1387. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1388. if (0 != t->index)
  1389. return -EINVAL;
  1390. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1391. return 0;
  1392. }
  1393. /*
  1394. * em28xx_v4l2_open()
  1395. * inits the device and starts isoc transfer
  1396. */
  1397. static int em28xx_v4l2_open(struct file *filp)
  1398. {
  1399. int errCode = 0, radio = 0;
  1400. struct video_device *vdev = video_devdata(filp);
  1401. struct em28xx *dev = video_drvdata(filp);
  1402. enum v4l2_buf_type fh_type = 0;
  1403. struct em28xx_fh *fh;
  1404. enum v4l2_field field;
  1405. switch (vdev->vfl_type) {
  1406. case VFL_TYPE_GRABBER:
  1407. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1408. break;
  1409. case VFL_TYPE_VBI:
  1410. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1411. break;
  1412. case VFL_TYPE_RADIO:
  1413. radio = 1;
  1414. break;
  1415. }
  1416. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1417. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1418. dev->users);
  1419. if (mutex_lock_interruptible(&dev->lock))
  1420. return -ERESTARTSYS;
  1421. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1422. if (!fh) {
  1423. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1424. mutex_unlock(&dev->lock);
  1425. return -ENOMEM;
  1426. }
  1427. fh->dev = dev;
  1428. fh->radio = radio;
  1429. fh->type = fh_type;
  1430. filp->private_data = fh;
  1431. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1432. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1433. em28xx_set_alternate(dev);
  1434. em28xx_resolution_set(dev);
  1435. /* Needed, since GPIO might have disabled power of
  1436. some i2c device
  1437. */
  1438. em28xx_wake_i2c(dev);
  1439. }
  1440. if (fh->radio) {
  1441. em28xx_videodbg("video_open: setting radio device\n");
  1442. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1443. }
  1444. dev->users++;
  1445. if (dev->progressive)
  1446. field = V4L2_FIELD_NONE;
  1447. else
  1448. field = V4L2_FIELD_INTERLACED;
  1449. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1450. NULL, &dev->slock,
  1451. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1452. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1453. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1454. NULL, &dev->slock,
  1455. V4L2_BUF_TYPE_VBI_CAPTURE,
  1456. V4L2_FIELD_SEQ_TB,
  1457. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1458. mutex_unlock(&dev->lock);
  1459. return errCode;
  1460. }
  1461. /*
  1462. * em28xx_realease_resources()
  1463. * unregisters the v4l2,i2c and usb devices
  1464. * called when the device gets disconected or at module unload
  1465. */
  1466. void em28xx_release_analog_resources(struct em28xx *dev)
  1467. {
  1468. /*FIXME: I2C IR should be disconnected */
  1469. if (dev->radio_dev) {
  1470. if (video_is_registered(dev->radio_dev))
  1471. video_unregister_device(dev->radio_dev);
  1472. else
  1473. video_device_release(dev->radio_dev);
  1474. dev->radio_dev = NULL;
  1475. }
  1476. if (dev->vbi_dev) {
  1477. em28xx_info("V4L2 device %s deregistered\n",
  1478. video_device_node_name(dev->vbi_dev));
  1479. if (video_is_registered(dev->vbi_dev))
  1480. video_unregister_device(dev->vbi_dev);
  1481. else
  1482. video_device_release(dev->vbi_dev);
  1483. dev->vbi_dev = NULL;
  1484. }
  1485. if (dev->vdev) {
  1486. em28xx_info("V4L2 device %s deregistered\n",
  1487. video_device_node_name(dev->vdev));
  1488. if (video_is_registered(dev->vdev))
  1489. video_unregister_device(dev->vdev);
  1490. else
  1491. video_device_release(dev->vdev);
  1492. dev->vdev = NULL;
  1493. }
  1494. }
  1495. /*
  1496. * em28xx_v4l2_close()
  1497. * stops streaming and deallocates all resources allocated by the v4l2
  1498. * calls and ioctls
  1499. */
  1500. static int em28xx_v4l2_close(struct file *filp)
  1501. {
  1502. struct em28xx_fh *fh = filp->private_data;
  1503. struct em28xx *dev = fh->dev;
  1504. int errCode;
  1505. em28xx_videodbg("users=%d\n", dev->users);
  1506. mutex_lock(&dev->lock);
  1507. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1508. videobuf_stop(&fh->vb_vidq);
  1509. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1510. }
  1511. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1512. videobuf_stop(&fh->vb_vbiq);
  1513. res_free(fh, EM28XX_RESOURCE_VBI);
  1514. }
  1515. if (dev->users == 1) {
  1516. /* the device is already disconnect,
  1517. free the remaining resources */
  1518. if (dev->state & DEV_DISCONNECTED) {
  1519. em28xx_release_resources(dev);
  1520. kfree(dev->alt_max_pkt_size_isoc);
  1521. mutex_unlock(&dev->lock);
  1522. kfree(dev);
  1523. kfree(fh);
  1524. return 0;
  1525. }
  1526. /* Save some power by putting tuner to sleep */
  1527. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1528. /* do this before setting alternate! */
  1529. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  1530. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1531. /* set alternate 0 */
  1532. dev->alt = 0;
  1533. em28xx_videodbg("setting alternate 0\n");
  1534. errCode = usb_set_interface(dev->udev, 0, 0);
  1535. if (errCode < 0) {
  1536. em28xx_errdev("cannot change alternate number to "
  1537. "0 (error=%i)\n", errCode);
  1538. }
  1539. }
  1540. videobuf_mmap_free(&fh->vb_vidq);
  1541. videobuf_mmap_free(&fh->vb_vbiq);
  1542. kfree(fh);
  1543. dev->users--;
  1544. mutex_unlock(&dev->lock);
  1545. return 0;
  1546. }
  1547. /*
  1548. * em28xx_v4l2_read()
  1549. * will allocate buffers when called for the first time
  1550. */
  1551. static ssize_t
  1552. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1553. loff_t *pos)
  1554. {
  1555. struct em28xx_fh *fh = filp->private_data;
  1556. struct em28xx *dev = fh->dev;
  1557. int rc;
  1558. rc = check_dev(dev);
  1559. if (rc < 0)
  1560. return rc;
  1561. if (mutex_lock_interruptible(&dev->lock))
  1562. return -ERESTARTSYS;
  1563. /* FIXME: read() is not prepared to allow changing the video
  1564. resolution while streaming. Seems a bug at em28xx_set_fmt
  1565. */
  1566. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1567. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1568. rc = -EBUSY;
  1569. else
  1570. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1571. filp->f_flags & O_NONBLOCK);
  1572. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1573. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1574. rc = -EBUSY;
  1575. else
  1576. rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1577. filp->f_flags & O_NONBLOCK);
  1578. }
  1579. mutex_unlock(&dev->lock);
  1580. return rc;
  1581. }
  1582. /*
  1583. * em28xx_poll()
  1584. * will allocate buffers when called for the first time
  1585. */
  1586. static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
  1587. {
  1588. struct em28xx_fh *fh = filp->private_data;
  1589. struct em28xx *dev = fh->dev;
  1590. int rc;
  1591. rc = check_dev(dev);
  1592. if (rc < 0)
  1593. return rc;
  1594. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1595. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1596. return POLLERR;
  1597. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1598. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1599. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1600. return POLLERR;
  1601. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1602. } else {
  1603. return POLLERR;
  1604. }
  1605. }
  1606. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1607. {
  1608. struct em28xx_fh *fh = filp->private_data;
  1609. struct em28xx *dev = fh->dev;
  1610. unsigned int res;
  1611. mutex_lock(&dev->lock);
  1612. res = em28xx_poll(filp, wait);
  1613. mutex_unlock(&dev->lock);
  1614. return res;
  1615. }
  1616. /*
  1617. * em28xx_v4l2_mmap()
  1618. */
  1619. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1620. {
  1621. struct em28xx_fh *fh = filp->private_data;
  1622. struct em28xx *dev = fh->dev;
  1623. int rc;
  1624. rc = check_dev(dev);
  1625. if (rc < 0)
  1626. return rc;
  1627. if (mutex_lock_interruptible(&dev->lock))
  1628. return -ERESTARTSYS;
  1629. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1630. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1631. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1632. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1633. mutex_unlock(&dev->lock);
  1634. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1635. (unsigned long)vma->vm_start,
  1636. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1637. rc);
  1638. return rc;
  1639. }
  1640. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1641. .owner = THIS_MODULE,
  1642. .open = em28xx_v4l2_open,
  1643. .release = em28xx_v4l2_close,
  1644. .read = em28xx_v4l2_read,
  1645. .poll = em28xx_v4l2_poll,
  1646. .mmap = em28xx_v4l2_mmap,
  1647. .unlocked_ioctl = video_ioctl2,
  1648. };
  1649. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1650. .vidioc_querycap = vidioc_querycap,
  1651. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1652. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1653. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1654. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1655. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1656. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1657. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1658. .vidioc_g_audio = vidioc_g_audio,
  1659. .vidioc_s_audio = vidioc_s_audio,
  1660. .vidioc_cropcap = vidioc_cropcap,
  1661. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1662. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1663. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1664. .vidioc_reqbufs = vidioc_reqbufs,
  1665. .vidioc_querybuf = vidioc_querybuf,
  1666. .vidioc_qbuf = vidioc_qbuf,
  1667. .vidioc_dqbuf = vidioc_dqbuf,
  1668. .vidioc_g_std = vidioc_g_std,
  1669. .vidioc_querystd = vidioc_querystd,
  1670. .vidioc_s_std = vidioc_s_std,
  1671. .vidioc_g_parm = vidioc_g_parm,
  1672. .vidioc_s_parm = vidioc_s_parm,
  1673. .vidioc_enum_input = vidioc_enum_input,
  1674. .vidioc_g_input = vidioc_g_input,
  1675. .vidioc_s_input = vidioc_s_input,
  1676. .vidioc_streamon = vidioc_streamon,
  1677. .vidioc_streamoff = vidioc_streamoff,
  1678. .vidioc_g_tuner = vidioc_g_tuner,
  1679. .vidioc_s_tuner = vidioc_s_tuner,
  1680. .vidioc_g_frequency = vidioc_g_frequency,
  1681. .vidioc_s_frequency = vidioc_s_frequency,
  1682. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1683. .vidioc_g_register = vidioc_g_register,
  1684. .vidioc_s_register = vidioc_s_register,
  1685. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1686. #endif
  1687. };
  1688. static const struct video_device em28xx_video_template = {
  1689. .fops = &em28xx_v4l_fops,
  1690. .release = video_device_release,
  1691. .ioctl_ops = &video_ioctl_ops,
  1692. .tvnorms = V4L2_STD_ALL,
  1693. .current_norm = V4L2_STD_PAL,
  1694. };
  1695. static const struct v4l2_file_operations radio_fops = {
  1696. .owner = THIS_MODULE,
  1697. .open = em28xx_v4l2_open,
  1698. .release = em28xx_v4l2_close,
  1699. .unlocked_ioctl = video_ioctl2,
  1700. };
  1701. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1702. .vidioc_querycap = vidioc_querycap,
  1703. .vidioc_g_tuner = radio_g_tuner,
  1704. .vidioc_s_tuner = radio_s_tuner,
  1705. .vidioc_g_frequency = vidioc_g_frequency,
  1706. .vidioc_s_frequency = vidioc_s_frequency,
  1707. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1708. .vidioc_g_register = vidioc_g_register,
  1709. .vidioc_s_register = vidioc_s_register,
  1710. #endif
  1711. };
  1712. static struct video_device em28xx_radio_template = {
  1713. .name = "em28xx-radio",
  1714. .fops = &radio_fops,
  1715. .ioctl_ops = &radio_ioctl_ops,
  1716. };
  1717. /******************************** usb interface ******************************/
  1718. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1719. const struct video_device *template,
  1720. const char *type_name)
  1721. {
  1722. struct video_device *vfd;
  1723. vfd = video_device_alloc();
  1724. if (NULL == vfd)
  1725. return NULL;
  1726. *vfd = *template;
  1727. vfd->v4l2_dev = &dev->v4l2_dev;
  1728. vfd->release = video_device_release;
  1729. vfd->debug = video_debug;
  1730. vfd->lock = &dev->lock;
  1731. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1732. dev->name, type_name);
  1733. video_set_drvdata(vfd, dev);
  1734. return vfd;
  1735. }
  1736. int em28xx_register_analog_devices(struct em28xx *dev)
  1737. {
  1738. u8 val;
  1739. int ret;
  1740. unsigned int maxw;
  1741. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  1742. dev->name, EM28XX_VERSION);
  1743. /* set default norm */
  1744. dev->norm = em28xx_video_template.current_norm;
  1745. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1746. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1747. /* Analog specific initialization */
  1748. dev->format = &format[0];
  1749. maxw = norm_maxw(dev);
  1750. /* MaxPacketSize for em2800 is too small to capture at full resolution
  1751. * use half of maxw as the scaler can only scale to 50% */
  1752. if (dev->board.is_em2800)
  1753. maxw /= 2;
  1754. em28xx_set_video_format(dev, format[0].fourcc,
  1755. maxw, norm_maxh(dev));
  1756. video_mux(dev, 0);
  1757. /* Audio defaults */
  1758. dev->mute = 1;
  1759. dev->volume = 0x1f;
  1760. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1761. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1762. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1763. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1764. em28xx_set_outfmt(dev);
  1765. em28xx_colorlevels_set_default(dev);
  1766. em28xx_compression_disable(dev);
  1767. /* allocate and fill video video_device struct */
  1768. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1769. if (!dev->vdev) {
  1770. em28xx_errdev("cannot allocate video_device.\n");
  1771. return -ENODEV;
  1772. }
  1773. /* register v4l2 video video_device */
  1774. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1775. video_nr[dev->devno]);
  1776. if (ret) {
  1777. em28xx_errdev("unable to register video device (error=%i).\n",
  1778. ret);
  1779. return ret;
  1780. }
  1781. /* Allocate and fill vbi video_device struct */
  1782. if (em28xx_vbi_supported(dev) == 1) {
  1783. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1784. "vbi");
  1785. /* register v4l2 vbi video_device */
  1786. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1787. vbi_nr[dev->devno]);
  1788. if (ret < 0) {
  1789. em28xx_errdev("unable to register vbi device\n");
  1790. return ret;
  1791. }
  1792. }
  1793. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1794. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1795. "radio");
  1796. if (!dev->radio_dev) {
  1797. em28xx_errdev("cannot allocate video_device.\n");
  1798. return -ENODEV;
  1799. }
  1800. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1801. radio_nr[dev->devno]);
  1802. if (ret < 0) {
  1803. em28xx_errdev("can't register radio device\n");
  1804. return ret;
  1805. }
  1806. em28xx_info("Registered radio device as %s\n",
  1807. video_device_node_name(dev->radio_dev));
  1808. }
  1809. em28xx_info("V4L2 video device registered as %s\n",
  1810. video_device_node_name(dev->vdev));
  1811. if (dev->vbi_dev)
  1812. em28xx_info("V4L2 VBI device registered as %s\n",
  1813. video_device_node_name(dev->vbi_dev));
  1814. return 0;
  1815. }