em28xx-video.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  4. Markus Rechberger <mrechberger@gmail.com>
  5. Mauro Carvalho Chehab <mchehab@infradead.org>
  6. Sascha Sommer <saschasommer@freenet.de>
  7. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  8. by Luca Risolia <luca.risolia@studio.unibo.it>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/bitmap.h>
  26. #include <linux/usb.h>
  27. #include <linux/i2c.h>
  28. #include <linux/version.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include "em28xx.h"
  32. #include <media/v4l2-common.h>
  33. #include <media/msp3400.h>
  34. #include <media/tuner.h>
  35. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  36. "Markus Rechberger <mrechberger@gmail.com>, " \
  37. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  38. "Sascha Sommer <saschasommer@freenet.de>"
  39. #define DRIVER_NAME "em28xx"
  40. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  41. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
  42. #define em28xx_videodbg(fmt, arg...) do {\
  43. if (video_debug) \
  44. printk(KERN_INFO "%s %s :"fmt, \
  45. dev->name, __func__ , ##arg); } while (0)
  46. static unsigned int isoc_debug;
  47. module_param(isoc_debug, int, 0644);
  48. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  49. #define em28xx_isocdbg(fmt, arg...) do {\
  50. if (isoc_debug) \
  51. printk(KERN_INFO "%s %s :"fmt, \
  52. dev->name, __func__ , ##arg); } while (0)
  53. /* Limits minimum and default number of buffers */
  54. #define EM28XX_MIN_BUF 4
  55. #define EM28XX_DEF_BUF 8
  56. MODULE_AUTHOR(DRIVER_AUTHOR);
  57. MODULE_DESCRIPTION(DRIVER_DESC);
  58. MODULE_LICENSE("GPL");
  59. static LIST_HEAD(em28xx_devlist);
  60. static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  61. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. module_param_array(card, int, NULL, 0444);
  65. module_param_array(video_nr, int, NULL, 0444);
  66. module_param_array(vbi_nr, int, NULL, 0444);
  67. module_param_array(radio_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(card, "card type");
  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. /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
  76. static unsigned long em28xx_devused;
  77. /* supported controls */
  78. /* Common to all boards */
  79. static struct v4l2_queryctrl em28xx_qctrl[] = {
  80. {
  81. .id = V4L2_CID_AUDIO_VOLUME,
  82. .type = V4L2_CTRL_TYPE_INTEGER,
  83. .name = "Volume",
  84. .minimum = 0x0,
  85. .maximum = 0x1f,
  86. .step = 0x1,
  87. .default_value = 0x1f,
  88. .flags = 0,
  89. },{
  90. .id = V4L2_CID_AUDIO_MUTE,
  91. .type = V4L2_CTRL_TYPE_BOOLEAN,
  92. .name = "Mute",
  93. .minimum = 0,
  94. .maximum = 1,
  95. .step = 1,
  96. .default_value = 1,
  97. .flags = 0,
  98. }
  99. };
  100. static struct usb_driver em28xx_usb_driver;
  101. /* ------------------------------------------------------------------
  102. DMA and thread functions
  103. ------------------------------------------------------------------*/
  104. /*
  105. * Announces that a buffer were filled and request the next
  106. */
  107. static inline void buffer_filled(struct em28xx *dev,
  108. struct em28xx_dmaqueue *dma_q,
  109. struct em28xx_buffer *buf)
  110. {
  111. /* Advice that buffer was filled */
  112. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  113. buf->vb.state = VIDEOBUF_DONE;
  114. buf->vb.field_count++;
  115. do_gettimeofday(&buf->vb.ts);
  116. dev->isoc_ctl.buf = NULL;
  117. list_del(&buf->vb.queue);
  118. wake_up(&buf->vb.done);
  119. }
  120. /*
  121. * Identify the buffer header type and properly handles
  122. */
  123. static void em28xx_copy_video(struct em28xx *dev,
  124. struct em28xx_dmaqueue *dma_q,
  125. struct em28xx_buffer *buf,
  126. unsigned char *p,
  127. unsigned char *outp, unsigned long len)
  128. {
  129. void *fieldstart, *startwrite, *startread;
  130. int linesdone, currlinedone, offset, lencopy, remain;
  131. if (dev->frame_size != buf->vb.size) {
  132. em28xx_errdev("size %i and buf.length %lu are different!\n",
  133. dev->frame_size, buf->vb.size);
  134. return;
  135. }
  136. if (dma_q->pos + len > buf->vb.size)
  137. len = buf->vb.size - dma_q->pos;
  138. if (p[0] != 0x88 && p[0] != 0x22) {
  139. em28xx_isocdbg("frame is not complete\n");
  140. len += 4;
  141. } else
  142. p += 4;
  143. startread = p;
  144. remain = len;
  145. /* Interlaces frame */
  146. if (buf->top_field)
  147. fieldstart = outp;
  148. else
  149. fieldstart = outp + dev->bytesperline;
  150. linesdone = dma_q->pos / dev->bytesperline;
  151. currlinedone = dma_q->pos % dev->bytesperline;
  152. offset = linesdone * dev->bytesperline * 2 + currlinedone;
  153. startwrite = fieldstart + offset;
  154. lencopy = dev->bytesperline - currlinedone;
  155. lencopy = lencopy > remain ? remain : lencopy;
  156. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  157. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  158. ((char *)startwrite + lencopy) -
  159. ((char *)outp + buf->vb.size));
  160. lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
  161. }
  162. if (lencopy <= 0)
  163. return;
  164. memcpy(startwrite, startread, lencopy);
  165. remain -= lencopy;
  166. while (remain > 0) {
  167. startwrite += lencopy + dev->bytesperline;
  168. startread += lencopy;
  169. if (dev->bytesperline > remain)
  170. lencopy = remain;
  171. else
  172. lencopy = dev->bytesperline;
  173. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  174. em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
  175. ((char *)startwrite + lencopy) -
  176. ((char *)outp + buf->vb.size));
  177. lencopy = remain = (char *)outp + buf->vb.size -
  178. (char *)startwrite;
  179. }
  180. if (lencopy <= 0)
  181. break;
  182. memcpy(startwrite, startread, lencopy);
  183. remain -= lencopy;
  184. }
  185. dma_q->pos += len;
  186. }
  187. static inline void print_err_status(struct em28xx *dev,
  188. int packet, int status)
  189. {
  190. char *errmsg = "Unknown";
  191. switch (status) {
  192. case -ENOENT:
  193. errmsg = "unlinked synchronuously";
  194. break;
  195. case -ECONNRESET:
  196. errmsg = "unlinked asynchronuously";
  197. break;
  198. case -ENOSR:
  199. errmsg = "Buffer error (overrun)";
  200. break;
  201. case -EPIPE:
  202. errmsg = "Stalled (device not responding)";
  203. break;
  204. case -EOVERFLOW:
  205. errmsg = "Babble (bad cable?)";
  206. break;
  207. case -EPROTO:
  208. errmsg = "Bit-stuff error (bad cable?)";
  209. break;
  210. case -EILSEQ:
  211. errmsg = "CRC/Timeout (could be anything)";
  212. break;
  213. case -ETIME:
  214. errmsg = "Device does not respond";
  215. break;
  216. }
  217. if (packet < 0) {
  218. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  219. } else {
  220. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  221. packet, status, errmsg);
  222. }
  223. }
  224. /*
  225. * video-buf generic routine to get the next available buffer
  226. */
  227. static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
  228. struct em28xx_buffer **buf)
  229. {
  230. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  231. char *outp;
  232. if (list_empty(&dma_q->active)) {
  233. em28xx_isocdbg("No active queue to serve\n");
  234. dev->isoc_ctl.buf = NULL;
  235. return 0;
  236. }
  237. /* Check if the last buffer were fully filled */
  238. *buf = dev->isoc_ctl.buf;
  239. /* Nobody is waiting on this buffer - discards */
  240. if (*buf && !waitqueue_active(&(*buf)->vb.done)) {
  241. dev->isoc_ctl.buf = NULL;
  242. *buf = NULL;
  243. }
  244. /* Returns the last buffer, to be filled with remaining data */
  245. if (*buf)
  246. return 1;
  247. /* Get the next buffer */
  248. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  249. /* Nobody is waiting on the next buffer. returns */
  250. if (!*buf || !waitqueue_active(&(*buf)->vb.done)) {
  251. em28xx_isocdbg("No active queue to serve\n");
  252. return 0;
  253. }
  254. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  255. outp = videobuf_to_vmalloc(&(*buf)->vb);
  256. memset(outp, 0, (*buf)->vb.size);
  257. dev->isoc_ctl.buf = *buf;
  258. return 1;
  259. }
  260. /*
  261. * Controls the isoc copy of each urb packet
  262. */
  263. static inline int em28xx_isoc_copy(struct urb *urb)
  264. {
  265. struct em28xx_buffer *buf;
  266. struct em28xx_dmaqueue *dma_q = urb->context;
  267. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  268. unsigned char *outp;
  269. int i, len = 0, rc = 1;
  270. unsigned char *p;
  271. if (!dev)
  272. return 0;
  273. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  274. return 0;
  275. if (urb->status < 0) {
  276. print_err_status(dev, -1, urb->status);
  277. if (urb->status == -ENOENT)
  278. return 0;
  279. }
  280. rc = get_next_buf(dma_q, &buf);
  281. if (rc <= 0)
  282. return rc;
  283. outp = videobuf_to_vmalloc(&buf->vb);
  284. for (i = 0; i < urb->number_of_packets; i++) {
  285. int status = urb->iso_frame_desc[i].status;
  286. if (status < 0) {
  287. print_err_status(dev, i, status);
  288. if (urb->iso_frame_desc[i].status != -EPROTO)
  289. continue;
  290. }
  291. len = urb->iso_frame_desc[i].actual_length - 4;
  292. if (urb->iso_frame_desc[i].actual_length <= 0) {
  293. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  294. continue;
  295. }
  296. if (urb->iso_frame_desc[i].actual_length >
  297. dev->max_pkt_size) {
  298. em28xx_isocdbg("packet bigger than packet size");
  299. continue;
  300. }
  301. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  302. /* FIXME: incomplete buffer checks where removed to make
  303. logic simpler. Impacts of those changes should be evaluated
  304. */
  305. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  306. em28xx_isocdbg("VBI HEADER!!!\n");
  307. /* FIXME: Should add vbi copy */
  308. continue;
  309. }
  310. if (p[0] == 0x22 && p[1] == 0x5a) {
  311. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  312. len, (p[2] & 1)? "odd" : "even");
  313. if (p[2] & 1)
  314. buf->top_field = 0;
  315. else
  316. buf->top_field = 1;
  317. // if (dev->isoc_ctl.last_field && !buf->top_field) {
  318. if (dev->isoc_ctl.last_field != buf->top_field) {
  319. buffer_filled(dev, dma_q, buf);
  320. rc = get_next_buf(dma_q, &buf);
  321. if (rc <= 0)
  322. return rc;
  323. outp = videobuf_to_vmalloc(&buf->vb);
  324. }
  325. dev->isoc_ctl.last_field = buf->top_field;
  326. dma_q->pos = 0;
  327. }
  328. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  329. }
  330. return rc;
  331. }
  332. /* ------------------------------------------------------------------
  333. URB control
  334. ------------------------------------------------------------------*/
  335. /*
  336. * IRQ callback, called by URB callback
  337. */
  338. static void em28xx_irq_callback(struct urb *urb)
  339. {
  340. struct em28xx_dmaqueue *dma_q = urb->context;
  341. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  342. int rc, i;
  343. /* Copy data from URB */
  344. spin_lock(&dev->slock);
  345. rc = em28xx_isoc_copy(urb);
  346. spin_unlock(&dev->slock);
  347. /* Reset urb buffers */
  348. for (i = 0; i < urb->number_of_packets; i++) {
  349. urb->iso_frame_desc[i].status = 0;
  350. urb->iso_frame_desc[i].actual_length = 0;
  351. }
  352. urb->status = 0;
  353. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  354. if (urb->status) {
  355. em28xx_err("urb resubmit failed (error=%i)\n",
  356. urb->status);
  357. }
  358. }
  359. /*
  360. * Stop and Deallocate URBs
  361. */
  362. static void em28xx_uninit_isoc(struct em28xx *dev)
  363. {
  364. struct urb *urb;
  365. int i;
  366. em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
  367. dev->isoc_ctl.nfields = -1;
  368. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  369. urb = dev->isoc_ctl.urb[i];
  370. if (urb) {
  371. usb_kill_urb(urb);
  372. usb_unlink_urb(urb);
  373. if (dev->isoc_ctl.transfer_buffer[i]) {
  374. usb_buffer_free(dev->udev,
  375. urb->transfer_buffer_length,
  376. dev->isoc_ctl.transfer_buffer[i],
  377. urb->transfer_dma);
  378. }
  379. usb_free_urb(urb);
  380. dev->isoc_ctl.urb[i] = NULL;
  381. }
  382. dev->isoc_ctl.transfer_buffer[i] = NULL;
  383. }
  384. kfree(dev->isoc_ctl.urb);
  385. kfree(dev->isoc_ctl.transfer_buffer);
  386. dev->isoc_ctl.urb = NULL;
  387. dev->isoc_ctl.transfer_buffer = NULL;
  388. dev->isoc_ctl.num_bufs = 0;
  389. em28xx_capture_start(dev, 0);
  390. }
  391. /*
  392. * Allocate URBs and start IRQ
  393. */
  394. static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
  395. int num_bufs)
  396. {
  397. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  398. int i;
  399. int sb_size, pipe;
  400. struct urb *urb;
  401. int j, k;
  402. em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
  403. /* De-allocates all pending stuff */
  404. em28xx_uninit_isoc(dev);
  405. dev->isoc_ctl.num_bufs = num_bufs;
  406. dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  407. if (!dev->isoc_ctl.urb) {
  408. em28xx_errdev("cannot alloc memory for usb buffers\n");
  409. return -ENOMEM;
  410. }
  411. dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
  412. GFP_KERNEL);
  413. if (!dev->isoc_ctl.urb) {
  414. em28xx_errdev("cannot allocate memory for usbtransfer\n");
  415. kfree(dev->isoc_ctl.urb);
  416. return -ENOMEM;
  417. }
  418. dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
  419. dev->isoc_ctl.buf = NULL;
  420. sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
  421. /* allocate urbs and transfer buffers */
  422. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  423. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  424. if (!urb) {
  425. em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
  426. em28xx_uninit_isoc(dev);
  427. return -ENOMEM;
  428. }
  429. dev->isoc_ctl.urb[i] = urb;
  430. dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
  431. sb_size, GFP_KERNEL, &urb->transfer_dma);
  432. if (!dev->isoc_ctl.transfer_buffer[i]) {
  433. em28xx_err("unable to allocate %i bytes for transfer"
  434. " buffer %i%s\n",
  435. sb_size, i,
  436. in_interrupt()?" while in int":"");
  437. em28xx_uninit_isoc(dev);
  438. return -ENOMEM;
  439. }
  440. memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
  441. /* FIXME: this is a hack - should be
  442. 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
  443. should also be using 'desc.bInterval'
  444. */
  445. pipe = usb_rcvisocpipe(dev->udev, 0x82);
  446. usb_fill_int_urb(urb, dev->udev, pipe,
  447. dev->isoc_ctl.transfer_buffer[i], sb_size,
  448. em28xx_irq_callback, dma_q, 1);
  449. urb->number_of_packets = max_packets;
  450. urb->transfer_flags = URB_ISO_ASAP;
  451. k = 0;
  452. for (j = 0; j < max_packets; j++) {
  453. urb->iso_frame_desc[j].offset = k;
  454. urb->iso_frame_desc[j].length =
  455. dev->isoc_ctl.max_pkt_size;
  456. k += dev->isoc_ctl.max_pkt_size;
  457. }
  458. }
  459. return 0;
  460. }
  461. static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
  462. {
  463. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  464. int i, rc = 0;
  465. em28xx_videodbg("Called em28xx_start_thread\n");
  466. init_waitqueue_head(&dma_q->wq);
  467. em28xx_capture_start(dev, 1);
  468. /* submit urbs and enables IRQ */
  469. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  470. rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  471. if (rc) {
  472. em28xx_err("submit of urb %i failed (error=%i)\n", i,
  473. rc);
  474. em28xx_uninit_isoc(dev);
  475. return rc;
  476. }
  477. }
  478. if (rc < 0)
  479. return rc;
  480. return 0;
  481. }
  482. /* ------------------------------------------------------------------
  483. Videobuf operations
  484. ------------------------------------------------------------------*/
  485. static int
  486. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  487. {
  488. struct em28xx_fh *fh = vq->priv_data;
  489. *size = 16 * fh->dev->width * fh->dev->height >> 3;
  490. if (0 == *count)
  491. *count = EM28XX_DEF_BUF;
  492. if (*count < EM28XX_MIN_BUF)
  493. *count = EM28XX_MIN_BUF;
  494. return 0;
  495. }
  496. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  497. {
  498. if (in_interrupt())
  499. BUG();
  500. videobuf_waiton(&buf->vb, 0, 0);
  501. videobuf_vmalloc_free(&buf->vb);
  502. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  503. }
  504. static int
  505. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  506. enum v4l2_field field)
  507. {
  508. struct em28xx_fh *fh = vq->priv_data;
  509. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  510. struct em28xx *dev = fh->dev;
  511. struct em28xx_dmaqueue *vidq = &dev->vidq;
  512. int rc = 0, urb_init = 0;
  513. /* BUG_ON(NULL == fh->fmt); */
  514. /* FIXME: It assumes depth = 16 */
  515. /* The only currently supported format is 16 bits/pixel */
  516. buf->vb.size = 16 * dev->width * dev->height >> 3;
  517. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  518. return -EINVAL;
  519. buf->fmt = fh->fmt;
  520. buf->vb.width = dev->width;
  521. buf->vb.height = dev->height;
  522. buf->vb.field = field;
  523. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  524. rc = videobuf_iolock(vq, &buf->vb, NULL);
  525. if (rc < 0)
  526. goto fail;
  527. }
  528. if (!dev->isoc_ctl.num_bufs)
  529. urb_init = 1;
  530. if (urb_init) {
  531. rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
  532. EM28XX_NUM_BUFS);
  533. if (rc < 0)
  534. goto fail;
  535. rc = em28xx_start_thread(vidq);
  536. if (rc < 0)
  537. goto fail;
  538. }
  539. buf->vb.state = VIDEOBUF_PREPARED;
  540. return 0;
  541. fail:
  542. free_buffer(vq, buf);
  543. return rc;
  544. }
  545. static void
  546. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  547. {
  548. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  549. struct em28xx_fh *fh = vq->priv_data;
  550. struct em28xx *dev = fh->dev;
  551. struct em28xx_dmaqueue *vidq = &dev->vidq;
  552. buf->vb.state = VIDEOBUF_QUEUED;
  553. list_add_tail(&buf->vb.queue, &vidq->active);
  554. }
  555. static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  556. {
  557. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  558. struct em28xx_fh *fh = vq->priv_data;
  559. struct em28xx *dev = (struct em28xx *)fh->dev;
  560. em28xx_isocdbg("em28xx: called buffer_release\n");
  561. free_buffer(vq, buf);
  562. }
  563. static struct videobuf_queue_ops em28xx_video_qops = {
  564. .buf_setup = buffer_setup,
  565. .buf_prepare = buffer_prepare,
  566. .buf_queue = buffer_queue,
  567. .buf_release = buffer_release,
  568. };
  569. /********************* v4l2 interface ******************************************/
  570. /*
  571. * em28xx_config()
  572. * inits registers with sane defaults
  573. */
  574. static int em28xx_config(struct em28xx *dev)
  575. {
  576. /* Sets I2C speed to 100 KHz */
  577. if (!dev->is_em2800)
  578. em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
  579. /* enable vbi capturing */
  580. /* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
  581. /* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
  582. em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
  583. dev->mute = 1; /* maybe not the right place... */
  584. dev->volume = 0x1f;
  585. em28xx_outfmt_set_yuv422(dev);
  586. em28xx_colorlevels_set_default(dev);
  587. em28xx_compression_disable(dev);
  588. return 0;
  589. }
  590. /*
  591. * em28xx_config_i2c()
  592. * configure i2c attached devices
  593. */
  594. static void em28xx_config_i2c(struct em28xx *dev)
  595. {
  596. struct v4l2_routing route;
  597. route.input = INPUT(dev->ctl_input)->vmux;
  598. route.output = 0;
  599. em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
  600. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  601. em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
  602. }
  603. static void video_mux(struct em28xx *dev, int index)
  604. {
  605. struct v4l2_routing route;
  606. route.input = INPUT(index)->vmux;
  607. route.output = 0;
  608. dev->ctl_input = index;
  609. dev->ctl_ainput = INPUT(index)->amux;
  610. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  611. if (dev->has_msp34xx) {
  612. if (dev->i2s_speed)
  613. em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
  614. route.input = dev->ctl_ainput;
  615. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  616. /* Note: this is msp3400 specific */
  617. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
  618. }
  619. em28xx_audio_analog_set(dev);
  620. }
  621. /* Usage lock check functions */
  622. static int res_get(struct em28xx_fh *fh)
  623. {
  624. struct em28xx *dev = fh->dev;
  625. int rc = 0;
  626. /* This instance already has stream_on */
  627. if (fh->stream_on)
  628. return rc;
  629. if (dev->stream_on)
  630. return -EINVAL;
  631. mutex_lock(&dev->lock);
  632. dev->stream_on = 1;
  633. fh->stream_on = 1;
  634. mutex_unlock(&dev->lock);
  635. return rc;
  636. }
  637. static int res_check(struct em28xx_fh *fh)
  638. {
  639. return (fh->stream_on);
  640. }
  641. static void res_free(struct em28xx_fh *fh)
  642. {
  643. struct em28xx *dev = fh->dev;
  644. mutex_lock(&dev->lock);
  645. fh->stream_on = 0;
  646. dev->stream_on = 0;
  647. mutex_unlock(&dev->lock);
  648. }
  649. /*
  650. * em28xx_get_ctrl()
  651. * return the current saturation, brightness or contrast, mute state
  652. */
  653. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  654. {
  655. switch (ctrl->id) {
  656. case V4L2_CID_AUDIO_MUTE:
  657. ctrl->value = dev->mute;
  658. return 0;
  659. case V4L2_CID_AUDIO_VOLUME:
  660. ctrl->value = dev->volume;
  661. return 0;
  662. default:
  663. return -EINVAL;
  664. }
  665. }
  666. /*
  667. * em28xx_set_ctrl()
  668. * mute or set new saturation, brightness or contrast
  669. */
  670. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  671. {
  672. switch (ctrl->id) {
  673. case V4L2_CID_AUDIO_MUTE:
  674. if (ctrl->value != dev->mute) {
  675. dev->mute = ctrl->value;
  676. return em28xx_audio_analog_set(dev);
  677. }
  678. return 0;
  679. case V4L2_CID_AUDIO_VOLUME:
  680. dev->volume = ctrl->value;
  681. return em28xx_audio_analog_set(dev);
  682. default:
  683. return -EINVAL;
  684. }
  685. }
  686. static int check_dev(struct em28xx *dev)
  687. {
  688. if (dev->state & DEV_DISCONNECTED) {
  689. em28xx_errdev("v4l2 ioctl: device not present\n");
  690. return -ENODEV;
  691. }
  692. if (dev->state & DEV_MISCONFIGURED) {
  693. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  694. "close and open it again\n");
  695. return -EIO;
  696. }
  697. return 0;
  698. }
  699. static void get_scale(struct em28xx *dev,
  700. unsigned int width, unsigned int height,
  701. unsigned int *hscale, unsigned int *vscale)
  702. {
  703. unsigned int maxw = norm_maxw(dev);
  704. unsigned int maxh = norm_maxh(dev);
  705. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  706. if (*hscale >= 0x4000)
  707. *hscale = 0x3fff;
  708. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  709. if (*vscale >= 0x4000)
  710. *vscale = 0x3fff;
  711. }
  712. /* ------------------------------------------------------------------
  713. IOCTL vidioc handling
  714. ------------------------------------------------------------------*/
  715. static int vidioc_g_fmt_cap(struct file *file, void *priv,
  716. struct v4l2_format *f)
  717. {
  718. struct em28xx_fh *fh = priv;
  719. struct em28xx *dev = fh->dev;
  720. mutex_lock(&dev->lock);
  721. f->fmt.pix.width = dev->width;
  722. f->fmt.pix.height = dev->height;
  723. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  724. f->fmt.pix.bytesperline = dev->bytesperline;
  725. f->fmt.pix.sizeimage = dev->frame_size;
  726. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  727. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  728. f->fmt.pix.field = dev->interlaced ?
  729. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  730. mutex_unlock(&dev->lock);
  731. return 0;
  732. }
  733. static int vidioc_try_fmt_cap(struct file *file, void *priv,
  734. struct v4l2_format *f)
  735. {
  736. struct em28xx_fh *fh = priv;
  737. struct em28xx *dev = fh->dev;
  738. int width = f->fmt.pix.width;
  739. int height = f->fmt.pix.height;
  740. unsigned int maxw = norm_maxw(dev);
  741. unsigned int maxh = norm_maxh(dev);
  742. unsigned int hscale, vscale;
  743. /* width must even because of the YUYV format
  744. height must be even because of interlacing */
  745. height &= 0xfffe;
  746. width &= 0xfffe;
  747. if (height < 32)
  748. height = 32;
  749. if (height > maxh)
  750. height = maxh;
  751. if (width < 48)
  752. width = 48;
  753. if (width > maxw)
  754. width = maxw;
  755. mutex_lock(&dev->lock);
  756. if (dev->is_em2800) {
  757. /* the em2800 can only scale down to 50% */
  758. if (height % (maxh / 2))
  759. height = maxh;
  760. if (width % (maxw / 2))
  761. width = maxw;
  762. /* according to empiatech support */
  763. /* the MaxPacketSize is to small to support */
  764. /* framesizes larger than 640x480 @ 30 fps */
  765. /* or 640x576 @ 25 fps. As this would cut */
  766. /* of a part of the image we prefer */
  767. /* 360x576 or 360x480 for now */
  768. if (width == maxw && height == maxh)
  769. width /= 2;
  770. }
  771. get_scale(dev, width, height, &hscale, &vscale);
  772. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  773. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  774. f->fmt.pix.width = width;
  775. f->fmt.pix.height = height;
  776. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  777. f->fmt.pix.bytesperline = width * 2;
  778. f->fmt.pix.sizeimage = width * 2 * height;
  779. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  780. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  781. mutex_unlock(&dev->lock);
  782. return 0;
  783. }
  784. static int vidioc_s_fmt_cap(struct file *file, void *priv,
  785. struct v4l2_format *f)
  786. {
  787. struct em28xx_fh *fh = priv;
  788. struct em28xx *dev = fh->dev;
  789. int rc;
  790. rc = check_dev(dev);
  791. if (rc < 0)
  792. return rc;
  793. vidioc_try_fmt_cap(file, priv, f);
  794. mutex_lock(&dev->lock);
  795. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  796. em28xx_errdev("%s queue busy\n", __func__);
  797. rc = -EBUSY;
  798. goto out;
  799. }
  800. if (dev->stream_on && !fh->stream_on) {
  801. em28xx_errdev("%s device in use by another fh\n", __func__);
  802. rc = -EBUSY;
  803. goto out;
  804. }
  805. /* set new image size */
  806. dev->width = f->fmt.pix.width;
  807. dev->height = f->fmt.pix.height;
  808. dev->frame_size = dev->width * dev->height * 2;
  809. dev->field_size = dev->frame_size >> 1;
  810. dev->bytesperline = dev->width * 2;
  811. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  812. em28xx_set_alternate(dev);
  813. em28xx_resolution_set(dev);
  814. rc = 0;
  815. out:
  816. mutex_unlock(&dev->lock);
  817. return rc;
  818. }
  819. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  820. {
  821. struct em28xx_fh *fh = priv;
  822. struct em28xx *dev = fh->dev;
  823. struct v4l2_format f;
  824. int rc;
  825. rc = check_dev(dev);
  826. if (rc < 0)
  827. return rc;
  828. mutex_lock(&dev->lock);
  829. dev->norm = *norm;
  830. mutex_unlock(&dev->lock);
  831. /* Adjusts width/height, if needed */
  832. f.fmt.pix.width = dev->width;
  833. f.fmt.pix.height = dev->height;
  834. vidioc_try_fmt_cap(file, priv, &f);
  835. mutex_lock(&dev->lock);
  836. /* set new image size */
  837. dev->width = f.fmt.pix.width;
  838. dev->height = f.fmt.pix.height;
  839. dev->frame_size = dev->width * dev->height * 2;
  840. dev->field_size = dev->frame_size >> 1;
  841. dev->bytesperline = dev->width * 2;
  842. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  843. em28xx_resolution_set(dev);
  844. em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
  845. mutex_unlock(&dev->lock);
  846. return 0;
  847. }
  848. static const char *iname[] = {
  849. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  850. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  851. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  852. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  853. [EM28XX_VMUX_SVIDEO] = "S-Video",
  854. [EM28XX_VMUX_TELEVISION] = "Television",
  855. [EM28XX_VMUX_CABLE] = "Cable TV",
  856. [EM28XX_VMUX_DVB] = "DVB",
  857. [EM28XX_VMUX_DEBUG] = "for debug only",
  858. };
  859. static int vidioc_enum_input(struct file *file, void *priv,
  860. struct v4l2_input *i)
  861. {
  862. struct em28xx_fh *fh = priv;
  863. struct em28xx *dev = fh->dev;
  864. unsigned int n;
  865. n = i->index;
  866. if (n >= MAX_EM28XX_INPUT)
  867. return -EINVAL;
  868. if (0 == INPUT(n)->type)
  869. return -EINVAL;
  870. i->index = n;
  871. i->type = V4L2_INPUT_TYPE_CAMERA;
  872. strcpy(i->name, iname[INPUT(n)->type]);
  873. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  874. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  875. i->type = V4L2_INPUT_TYPE_TUNER;
  876. i->std = dev->vdev->tvnorms;
  877. return 0;
  878. }
  879. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  880. {
  881. struct em28xx_fh *fh = priv;
  882. struct em28xx *dev = fh->dev;
  883. *i = dev->ctl_input;
  884. return 0;
  885. }
  886. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  887. {
  888. struct em28xx_fh *fh = priv;
  889. struct em28xx *dev = fh->dev;
  890. int rc;
  891. rc = check_dev(dev);
  892. if (rc < 0)
  893. return rc;
  894. if (i >= MAX_EM28XX_INPUT)
  895. return -EINVAL;
  896. if (0 == INPUT(i)->type)
  897. return -EINVAL;
  898. mutex_lock(&dev->lock);
  899. video_mux(dev, i);
  900. mutex_unlock(&dev->lock);
  901. return 0;
  902. }
  903. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  904. {
  905. struct em28xx_fh *fh = priv;
  906. struct em28xx *dev = fh->dev;
  907. unsigned int index = a->index;
  908. if (a->index > 1)
  909. return -EINVAL;
  910. index = dev->ctl_ainput;
  911. if (index == 0) {
  912. strcpy(a->name, "Television");
  913. } else {
  914. strcpy(a->name, "Line In");
  915. }
  916. a->capability = V4L2_AUDCAP_STEREO;
  917. a->index = index;
  918. return 0;
  919. }
  920. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  921. {
  922. struct em28xx_fh *fh = priv;
  923. struct em28xx *dev = fh->dev;
  924. if (a->index != dev->ctl_ainput)
  925. return -EINVAL;
  926. return 0;
  927. }
  928. static int vidioc_queryctrl(struct file *file, void *priv,
  929. struct v4l2_queryctrl *qc)
  930. {
  931. struct em28xx_fh *fh = priv;
  932. struct em28xx *dev = fh->dev;
  933. int id = qc->id;
  934. int i;
  935. int rc;
  936. rc = check_dev(dev);
  937. if (rc < 0)
  938. return rc;
  939. memset(qc, 0, sizeof(*qc));
  940. qc->id = id;
  941. if (!dev->has_msp34xx) {
  942. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  943. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  944. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  945. return 0;
  946. }
  947. }
  948. }
  949. mutex_lock(&dev->lock);
  950. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  951. mutex_unlock(&dev->lock);
  952. if (qc->type)
  953. return 0;
  954. else
  955. return -EINVAL;
  956. }
  957. static int vidioc_g_ctrl(struct file *file, void *priv,
  958. struct v4l2_control *ctrl)
  959. {
  960. struct em28xx_fh *fh = priv;
  961. struct em28xx *dev = fh->dev;
  962. int rc;
  963. rc = check_dev(dev);
  964. if (rc < 0)
  965. return rc;
  966. mutex_lock(&dev->lock);
  967. if (!dev->has_msp34xx)
  968. rc = em28xx_get_ctrl(dev, ctrl);
  969. else
  970. rc = -EINVAL;
  971. if (rc == -EINVAL) {
  972. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  973. rc = 0;
  974. }
  975. mutex_unlock(&dev->lock);
  976. return rc;
  977. }
  978. static int vidioc_s_ctrl(struct file *file, void *priv,
  979. struct v4l2_control *ctrl)
  980. {
  981. struct em28xx_fh *fh = priv;
  982. struct em28xx *dev = fh->dev;
  983. u8 i;
  984. int rc;
  985. rc = check_dev(dev);
  986. if (rc < 0)
  987. return rc;
  988. mutex_lock(&dev->lock);
  989. if (dev->has_msp34xx)
  990. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  991. else {
  992. rc = 1;
  993. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  994. if (ctrl->id == em28xx_qctrl[i].id) {
  995. if (ctrl->value < em28xx_qctrl[i].minimum ||
  996. ctrl->value > em28xx_qctrl[i].maximum) {
  997. rc = -ERANGE;
  998. break;
  999. }
  1000. rc = em28xx_set_ctrl(dev, ctrl);
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. /* Control not found - try to send it to the attached devices */
  1006. if (rc == 1) {
  1007. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  1008. rc = 0;
  1009. }
  1010. mutex_unlock(&dev->lock);
  1011. return rc;
  1012. }
  1013. static int vidioc_g_tuner(struct file *file, void *priv,
  1014. struct v4l2_tuner *t)
  1015. {
  1016. struct em28xx_fh *fh = priv;
  1017. struct em28xx *dev = fh->dev;
  1018. int rc;
  1019. rc = check_dev(dev);
  1020. if (rc < 0)
  1021. return rc;
  1022. if (0 != t->index)
  1023. return -EINVAL;
  1024. strcpy(t->name, "Tuner");
  1025. mutex_lock(&dev->lock);
  1026. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1027. mutex_unlock(&dev->lock);
  1028. return 0;
  1029. }
  1030. static int vidioc_s_tuner(struct file *file, void *priv,
  1031. struct v4l2_tuner *t)
  1032. {
  1033. struct em28xx_fh *fh = priv;
  1034. struct em28xx *dev = fh->dev;
  1035. int rc;
  1036. rc = check_dev(dev);
  1037. if (rc < 0)
  1038. return rc;
  1039. if (0 != t->index)
  1040. return -EINVAL;
  1041. mutex_lock(&dev->lock);
  1042. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1043. mutex_unlock(&dev->lock);
  1044. return 0;
  1045. }
  1046. static int vidioc_g_frequency(struct file *file, void *priv,
  1047. struct v4l2_frequency *f)
  1048. {
  1049. struct em28xx_fh *fh = priv;
  1050. struct em28xx *dev = fh->dev;
  1051. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1052. f->frequency = dev->ctl_freq;
  1053. return 0;
  1054. }
  1055. static int vidioc_s_frequency(struct file *file, void *priv,
  1056. struct v4l2_frequency *f)
  1057. {
  1058. struct em28xx_fh *fh = priv;
  1059. struct em28xx *dev = fh->dev;
  1060. int rc;
  1061. rc = check_dev(dev);
  1062. if (rc < 0)
  1063. return rc;
  1064. if (0 != f->tuner)
  1065. return -EINVAL;
  1066. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1067. return -EINVAL;
  1068. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1069. return -EINVAL;
  1070. mutex_lock(&dev->lock);
  1071. dev->ctl_freq = f->frequency;
  1072. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  1073. mutex_unlock(&dev->lock);
  1074. return 0;
  1075. }
  1076. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1077. static int em28xx_reg_len(int reg)
  1078. {
  1079. switch (reg) {
  1080. case AC97LSB_REG:
  1081. case HSCALELOW_REG:
  1082. case VSCALELOW_REG:
  1083. return 2;
  1084. default:
  1085. return 1;
  1086. }
  1087. }
  1088. static int vidioc_g_register(struct file *file, void *priv,
  1089. struct v4l2_register *reg)
  1090. {
  1091. struct em28xx_fh *fh = priv;
  1092. struct em28xx *dev = fh->dev;
  1093. int ret;
  1094. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1095. return -EINVAL;
  1096. if (em28xx_reg_len(reg->reg) == 1) {
  1097. ret = em28xx_read_reg(dev, reg->reg);
  1098. if (ret < 0)
  1099. return ret;
  1100. reg->val = ret;
  1101. } else {
  1102. u64 val = 0;
  1103. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1104. reg->reg, (char *)&val, 2);
  1105. if (ret < 0)
  1106. return ret;
  1107. reg->val = cpu_to_le64((__u64)val);
  1108. }
  1109. return 0;
  1110. }
  1111. static int vidioc_s_register(struct file *file, void *priv,
  1112. struct v4l2_register *reg)
  1113. {
  1114. struct em28xx_fh *fh = priv;
  1115. struct em28xx *dev = fh->dev;
  1116. u64 buf;
  1117. buf = le64_to_cpu((__u64)reg->val);
  1118. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1119. em28xx_reg_len(reg->reg));
  1120. }
  1121. #endif
  1122. static int vidioc_cropcap(struct file *file, void *priv,
  1123. struct v4l2_cropcap *cc)
  1124. {
  1125. struct em28xx_fh *fh = priv;
  1126. struct em28xx *dev = fh->dev;
  1127. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1128. return -EINVAL;
  1129. cc->bounds.left = 0;
  1130. cc->bounds.top = 0;
  1131. cc->bounds.width = dev->width;
  1132. cc->bounds.height = dev->height;
  1133. cc->defrect = cc->bounds;
  1134. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1135. cc->pixelaspect.denominator = 59;
  1136. return 0;
  1137. }
  1138. static int vidioc_streamon(struct file *file, void *priv,
  1139. enum v4l2_buf_type type)
  1140. {
  1141. struct em28xx_fh *fh = priv;
  1142. struct em28xx *dev = fh->dev;
  1143. int rc;
  1144. rc = check_dev(dev);
  1145. if (rc < 0)
  1146. return rc;
  1147. if (unlikely(res_get(fh) < 0))
  1148. return -EBUSY;
  1149. return (videobuf_streamon(&fh->vb_vidq));
  1150. }
  1151. static int vidioc_streamoff(struct file *file, void *priv,
  1152. enum v4l2_buf_type type)
  1153. {
  1154. struct em28xx_fh *fh = priv;
  1155. struct em28xx *dev = fh->dev;
  1156. int rc;
  1157. rc = check_dev(dev);
  1158. if (rc < 0)
  1159. return rc;
  1160. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1161. return -EINVAL;
  1162. if (type != fh->type)
  1163. return -EINVAL;
  1164. videobuf_streamoff(&fh->vb_vidq);
  1165. res_free(fh);
  1166. return 0;
  1167. }
  1168. static int vidioc_querycap(struct file *file, void *priv,
  1169. struct v4l2_capability *cap)
  1170. {
  1171. struct em28xx_fh *fh = priv;
  1172. struct em28xx *dev = fh->dev;
  1173. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1174. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1175. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1176. cap->version = EM28XX_VERSION_CODE;
  1177. cap->capabilities =
  1178. V4L2_CAP_SLICED_VBI_CAPTURE |
  1179. V4L2_CAP_VIDEO_CAPTURE |
  1180. V4L2_CAP_AUDIO |
  1181. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1182. if (dev->tuner_type != TUNER_ABSENT)
  1183. cap->capabilities |= V4L2_CAP_TUNER;
  1184. return 0;
  1185. }
  1186. static int vidioc_enum_fmt_cap(struct file *file, void *priv,
  1187. struct v4l2_fmtdesc *fmtd)
  1188. {
  1189. if (fmtd->index != 0)
  1190. return -EINVAL;
  1191. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1192. strcpy(fmtd->description, "Packed YUY2");
  1193. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1194. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1195. return 0;
  1196. }
  1197. /* Sliced VBI ioctls */
  1198. static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
  1199. struct v4l2_format *f)
  1200. {
  1201. struct em28xx_fh *fh = priv;
  1202. struct em28xx *dev = fh->dev;
  1203. int rc;
  1204. rc = check_dev(dev);
  1205. if (rc < 0)
  1206. return rc;
  1207. mutex_lock(&dev->lock);
  1208. f->fmt.sliced.service_set = 0;
  1209. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1210. if (f->fmt.sliced.service_set == 0)
  1211. rc = -EINVAL;
  1212. mutex_unlock(&dev->lock);
  1213. return rc;
  1214. }
  1215. static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
  1216. struct v4l2_format *f)
  1217. {
  1218. struct em28xx_fh *fh = priv;
  1219. struct em28xx *dev = fh->dev;
  1220. int rc;
  1221. rc = check_dev(dev);
  1222. if (rc < 0)
  1223. return rc;
  1224. mutex_lock(&dev->lock);
  1225. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1226. mutex_unlock(&dev->lock);
  1227. if (f->fmt.sliced.service_set == 0)
  1228. return -EINVAL;
  1229. return 0;
  1230. }
  1231. static int vidioc_reqbufs(struct file *file, void *priv,
  1232. struct v4l2_requestbuffers *rb)
  1233. {
  1234. struct em28xx_fh *fh = priv;
  1235. struct em28xx *dev = fh->dev;
  1236. int rc;
  1237. rc = check_dev(dev);
  1238. if (rc < 0)
  1239. return rc;
  1240. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1241. }
  1242. static int vidioc_querybuf(struct file *file, void *priv,
  1243. struct v4l2_buffer *b)
  1244. {
  1245. struct em28xx_fh *fh = priv;
  1246. struct em28xx *dev = fh->dev;
  1247. int rc;
  1248. rc = check_dev(dev);
  1249. if (rc < 0)
  1250. return rc;
  1251. return (videobuf_querybuf(&fh->vb_vidq, b));
  1252. }
  1253. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1254. {
  1255. struct em28xx_fh *fh = priv;
  1256. struct em28xx *dev = fh->dev;
  1257. int rc;
  1258. rc = check_dev(dev);
  1259. if (rc < 0)
  1260. return rc;
  1261. return (videobuf_qbuf(&fh->vb_vidq, b));
  1262. }
  1263. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1264. {
  1265. struct em28xx_fh *fh = priv;
  1266. struct em28xx *dev = fh->dev;
  1267. int rc;
  1268. rc = check_dev(dev);
  1269. if (rc < 0)
  1270. return rc;
  1271. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1272. file->f_flags & O_NONBLOCK));
  1273. }
  1274. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1275. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1276. {
  1277. struct em28xx_fh *fh = priv;
  1278. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1279. }
  1280. #endif
  1281. /* ----------------------------------------------------------- */
  1282. /* RADIO ESPECIFIC IOCTLS */
  1283. /* ----------------------------------------------------------- */
  1284. static int radio_querycap(struct file *file, void *priv,
  1285. struct v4l2_capability *cap)
  1286. {
  1287. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1288. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1289. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1290. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1291. cap->version = EM28XX_VERSION_CODE;
  1292. cap->capabilities = V4L2_CAP_TUNER;
  1293. return 0;
  1294. }
  1295. static int radio_g_tuner(struct file *file, void *priv,
  1296. struct v4l2_tuner *t)
  1297. {
  1298. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1299. if (unlikely(t->index > 0))
  1300. return -EINVAL;
  1301. strcpy(t->name, "Radio");
  1302. t->type = V4L2_TUNER_RADIO;
  1303. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1304. return 0;
  1305. }
  1306. static int radio_enum_input(struct file *file, void *priv,
  1307. struct v4l2_input *i)
  1308. {
  1309. if (i->index != 0)
  1310. return -EINVAL;
  1311. strcpy(i->name, "Radio");
  1312. i->type = V4L2_INPUT_TYPE_TUNER;
  1313. return 0;
  1314. }
  1315. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1316. {
  1317. if (unlikely(a->index))
  1318. return -EINVAL;
  1319. strcpy(a->name, "Radio");
  1320. return 0;
  1321. }
  1322. static int radio_s_tuner(struct file *file, void *priv,
  1323. struct v4l2_tuner *t)
  1324. {
  1325. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1326. if (0 != t->index)
  1327. return -EINVAL;
  1328. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1329. return 0;
  1330. }
  1331. static int radio_s_audio(struct file *file, void *fh,
  1332. struct v4l2_audio *a)
  1333. {
  1334. return 0;
  1335. }
  1336. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1337. {
  1338. return 0;
  1339. }
  1340. static int radio_queryctrl(struct file *file, void *priv,
  1341. struct v4l2_queryctrl *qc)
  1342. {
  1343. int i;
  1344. if (qc->id < V4L2_CID_BASE ||
  1345. qc->id >= V4L2_CID_LASTP1)
  1346. return -EINVAL;
  1347. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1348. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1349. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1350. return 0;
  1351. }
  1352. }
  1353. return -EINVAL;
  1354. }
  1355. /*
  1356. * em28xx_v4l2_open()
  1357. * inits the device and starts isoc transfer
  1358. */
  1359. static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
  1360. {
  1361. int minor = iminor(inode);
  1362. int errCode = 0, radio = 0;
  1363. struct em28xx *h,*dev = NULL;
  1364. struct em28xx_fh *fh;
  1365. enum v4l2_buf_type fh_type = 0;
  1366. list_for_each_entry(h, &em28xx_devlist, devlist) {
  1367. if (h->vdev->minor == minor) {
  1368. dev = h;
  1369. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1370. }
  1371. if (h->vbi_dev->minor == minor) {
  1372. dev = h;
  1373. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1374. }
  1375. if (h->radio_dev &&
  1376. h->radio_dev->minor == minor) {
  1377. radio = 1;
  1378. dev = h;
  1379. }
  1380. }
  1381. if (NULL == dev)
  1382. return -ENODEV;
  1383. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1384. minor, v4l2_type_names[fh_type], dev->users);
  1385. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1386. if (!fh) {
  1387. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1388. return -ENOMEM;
  1389. }
  1390. mutex_lock(&dev->lock);
  1391. fh->dev = dev;
  1392. fh->radio = radio;
  1393. fh->type = fh_type;
  1394. filp->private_data = fh;
  1395. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1396. dev->width = norm_maxw(dev);
  1397. dev->height = norm_maxh(dev);
  1398. dev->frame_size = dev->width * dev->height * 2;
  1399. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  1400. dev->bytesperline = dev->width * 2;
  1401. dev->hscale = 0;
  1402. dev->vscale = 0;
  1403. em28xx_set_alternate(dev);
  1404. em28xx_resolution_set(dev);
  1405. }
  1406. if (fh->radio) {
  1407. em28xx_videodbg("video_open: setting radio device\n");
  1408. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1409. }
  1410. dev->users++;
  1411. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1412. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1413. sizeof(struct em28xx_buffer), fh);
  1414. mutex_unlock(&dev->lock);
  1415. return errCode;
  1416. }
  1417. /*
  1418. * em28xx_realease_resources()
  1419. * unregisters the v4l2,i2c and usb devices
  1420. * called when the device gets disconected or at module unload
  1421. */
  1422. static void em28xx_release_resources(struct em28xx *dev)
  1423. {
  1424. /*FIXME: I2C IR should be disconnected */
  1425. em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
  1426. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1427. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1428. list_del(&dev->devlist);
  1429. if (dev->radio_dev) {
  1430. if (-1 != dev->radio_dev->minor)
  1431. video_unregister_device(dev->radio_dev);
  1432. else
  1433. video_device_release(dev->radio_dev);
  1434. dev->radio_dev = NULL;
  1435. }
  1436. if (dev->vbi_dev) {
  1437. if (-1 != dev->vbi_dev->minor)
  1438. video_unregister_device(dev->vbi_dev);
  1439. else
  1440. video_device_release(dev->vbi_dev);
  1441. dev->vbi_dev = NULL;
  1442. }
  1443. if (dev->vdev) {
  1444. if (-1 != dev->vdev->minor)
  1445. video_unregister_device(dev->vdev);
  1446. else
  1447. video_device_release(dev->vdev);
  1448. dev->vdev = NULL;
  1449. }
  1450. em28xx_i2c_unregister(dev);
  1451. usb_put_dev(dev->udev);
  1452. /* Mark device as unused */
  1453. em28xx_devused&=~(1<<dev->devno);
  1454. }
  1455. /*
  1456. * em28xx_v4l2_close()
  1457. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  1458. */
  1459. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  1460. {
  1461. struct em28xx_fh *fh = filp->private_data;
  1462. struct em28xx *dev = fh->dev;
  1463. int errCode;
  1464. em28xx_videodbg("users=%d\n", dev->users);
  1465. if (res_check(fh))
  1466. res_free(fh);
  1467. mutex_lock(&dev->lock);
  1468. if (dev->users == 1) {
  1469. videobuf_stop(&fh->vb_vidq);
  1470. videobuf_mmap_free(&fh->vb_vidq);
  1471. /* the device is already disconnect,
  1472. free the remaining resources */
  1473. if (dev->state & DEV_DISCONNECTED) {
  1474. em28xx_release_resources(dev);
  1475. mutex_unlock(&dev->lock);
  1476. kfree(dev);
  1477. return 0;
  1478. }
  1479. /* do this before setting alternate! */
  1480. em28xx_uninit_isoc(dev);
  1481. /* set alternate 0 */
  1482. dev->alt = 0;
  1483. em28xx_videodbg("setting alternate 0\n");
  1484. errCode = usb_set_interface(dev->udev, 0, 0);
  1485. if (errCode < 0) {
  1486. em28xx_errdev("cannot change alternate number to "
  1487. "0 (error=%i)\n", errCode);
  1488. }
  1489. }
  1490. kfree(fh);
  1491. dev->users--;
  1492. wake_up_interruptible_nr(&dev->open, 1);
  1493. mutex_unlock(&dev->lock);
  1494. return 0;
  1495. }
  1496. /*
  1497. * em28xx_v4l2_read()
  1498. * will allocate buffers when called for the first time
  1499. */
  1500. static ssize_t
  1501. em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
  1502. loff_t *pos)
  1503. {
  1504. struct em28xx_fh *fh = filp->private_data;
  1505. struct em28xx *dev = fh->dev;
  1506. int rc;
  1507. rc = check_dev(dev);
  1508. if (rc < 0)
  1509. return rc;
  1510. /* FIXME: read() is not prepared to allow changing the video
  1511. resolution while streaming. Seems a bug at em28xx_set_fmt
  1512. */
  1513. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1514. if (unlikely(res_get(fh)))
  1515. return -EBUSY;
  1516. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1517. filp->f_flags & O_NONBLOCK);
  1518. }
  1519. return 0;
  1520. }
  1521. /*
  1522. * em28xx_v4l2_poll()
  1523. * will allocate buffers when called for the first time
  1524. */
  1525. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1526. {
  1527. struct em28xx_fh *fh = filp->private_data;
  1528. struct em28xx *dev = fh->dev;
  1529. int rc;
  1530. rc = check_dev(dev);
  1531. if (rc < 0)
  1532. return rc;
  1533. if (unlikely(res_get(fh) < 0))
  1534. return POLLERR;
  1535. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1536. return POLLERR;
  1537. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1538. }
  1539. /*
  1540. * em28xx_v4l2_mmap()
  1541. */
  1542. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1543. {
  1544. struct em28xx_fh *fh = filp->private_data;
  1545. struct em28xx *dev = fh->dev;
  1546. int rc;
  1547. if (unlikely(res_get(fh) < 0))
  1548. return -EBUSY;
  1549. rc = check_dev(dev);
  1550. if (rc < 0)
  1551. return rc;
  1552. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1553. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1554. (unsigned long)vma->vm_start,
  1555. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1556. rc);
  1557. return rc;
  1558. }
  1559. static const struct file_operations em28xx_v4l_fops = {
  1560. .owner = THIS_MODULE,
  1561. .open = em28xx_v4l2_open,
  1562. .release = em28xx_v4l2_close,
  1563. .read = em28xx_v4l2_read,
  1564. .poll = em28xx_v4l2_poll,
  1565. .mmap = em28xx_v4l2_mmap,
  1566. .ioctl = video_ioctl2,
  1567. .llseek = no_llseek,
  1568. .compat_ioctl = v4l_compat_ioctl32,
  1569. };
  1570. static const struct file_operations radio_fops = {
  1571. .owner = THIS_MODULE,
  1572. .open = em28xx_v4l2_open,
  1573. .release = em28xx_v4l2_close,
  1574. .ioctl = video_ioctl2,
  1575. .compat_ioctl = v4l_compat_ioctl32,
  1576. .llseek = no_llseek,
  1577. };
  1578. static const struct video_device em28xx_video_template = {
  1579. .fops = &em28xx_v4l_fops,
  1580. .release = video_device_release,
  1581. .minor = -1,
  1582. .vidioc_querycap = vidioc_querycap,
  1583. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1584. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1585. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1586. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1587. .vidioc_g_audio = vidioc_g_audio,
  1588. .vidioc_s_audio = vidioc_s_audio,
  1589. .vidioc_cropcap = vidioc_cropcap,
  1590. .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
  1591. .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
  1592. .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
  1593. .vidioc_reqbufs = vidioc_reqbufs,
  1594. .vidioc_querybuf = vidioc_querybuf,
  1595. .vidioc_qbuf = vidioc_qbuf,
  1596. .vidioc_dqbuf = vidioc_dqbuf,
  1597. .vidioc_s_std = vidioc_s_std,
  1598. .vidioc_enum_input = vidioc_enum_input,
  1599. .vidioc_g_input = vidioc_g_input,
  1600. .vidioc_s_input = vidioc_s_input,
  1601. .vidioc_queryctrl = vidioc_queryctrl,
  1602. .vidioc_g_ctrl = vidioc_g_ctrl,
  1603. .vidioc_s_ctrl = vidioc_s_ctrl,
  1604. .vidioc_streamon = vidioc_streamon,
  1605. .vidioc_streamoff = vidioc_streamoff,
  1606. .vidioc_g_tuner = vidioc_g_tuner,
  1607. .vidioc_s_tuner = vidioc_s_tuner,
  1608. .vidioc_g_frequency = vidioc_g_frequency,
  1609. .vidioc_s_frequency = vidioc_s_frequency,
  1610. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1611. .vidioc_g_register = vidioc_g_register,
  1612. .vidioc_s_register = vidioc_s_register,
  1613. #endif
  1614. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1615. .vidiocgmbuf = vidiocgmbuf,
  1616. #endif
  1617. .tvnorms = V4L2_STD_ALL,
  1618. .current_norm = V4L2_STD_PAL,
  1619. };
  1620. static struct video_device em28xx_radio_template = {
  1621. .name = "em28xx-radio",
  1622. .type = VID_TYPE_TUNER,
  1623. .fops = &radio_fops,
  1624. .minor = -1,
  1625. .vidioc_querycap = radio_querycap,
  1626. .vidioc_g_tuner = radio_g_tuner,
  1627. .vidioc_enum_input = radio_enum_input,
  1628. .vidioc_g_audio = radio_g_audio,
  1629. .vidioc_s_tuner = radio_s_tuner,
  1630. .vidioc_s_audio = radio_s_audio,
  1631. .vidioc_s_input = radio_s_input,
  1632. .vidioc_queryctrl = radio_queryctrl,
  1633. .vidioc_g_ctrl = vidioc_g_ctrl,
  1634. .vidioc_s_ctrl = vidioc_s_ctrl,
  1635. .vidioc_g_frequency = vidioc_g_frequency,
  1636. .vidioc_s_frequency = vidioc_s_frequency,
  1637. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1638. .vidioc_g_register = vidioc_g_register,
  1639. .vidioc_s_register = vidioc_s_register,
  1640. #endif
  1641. };
  1642. /******************************** usb interface *****************************************/
  1643. static LIST_HEAD(em28xx_extension_devlist);
  1644. static DEFINE_MUTEX(em28xx_extension_devlist_lock);
  1645. int em28xx_register_extension(struct em28xx_ops *ops)
  1646. {
  1647. struct em28xx *h, *dev = NULL;
  1648. list_for_each_entry(h, &em28xx_devlist, devlist)
  1649. dev = h;
  1650. mutex_lock(&em28xx_extension_devlist_lock);
  1651. list_add_tail(&ops->next, &em28xx_extension_devlist);
  1652. if (dev)
  1653. ops->init(dev);
  1654. printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
  1655. mutex_unlock(&em28xx_extension_devlist_lock);
  1656. return 0;
  1657. }
  1658. EXPORT_SYMBOL(em28xx_register_extension);
  1659. void em28xx_unregister_extension(struct em28xx_ops *ops)
  1660. {
  1661. struct em28xx *h, *dev = NULL;
  1662. list_for_each_entry(h, &em28xx_devlist, devlist)
  1663. dev = h;
  1664. if (dev)
  1665. ops->fini(dev);
  1666. mutex_lock(&em28xx_extension_devlist_lock);
  1667. printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
  1668. list_del(&ops->next);
  1669. mutex_unlock(&em28xx_extension_devlist_lock);
  1670. }
  1671. EXPORT_SYMBOL(em28xx_unregister_extension);
  1672. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1673. const struct video_device *template,
  1674. const int type,
  1675. const char *type_name)
  1676. {
  1677. struct video_device *vfd;
  1678. vfd = video_device_alloc();
  1679. if (NULL == vfd)
  1680. return NULL;
  1681. *vfd = *template;
  1682. vfd->minor = -1;
  1683. vfd->dev = &dev->udev->dev;
  1684. vfd->release = video_device_release;
  1685. vfd->type = type;
  1686. vfd->debug = video_debug;
  1687. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1688. dev->name, type_name);
  1689. return vfd;
  1690. }
  1691. /*
  1692. * em28xx_init_dev()
  1693. * allocates and inits the device structs, registers i2c bus and v4l device
  1694. */
  1695. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1696. int minor)
  1697. {
  1698. struct em28xx_ops *ops = NULL;
  1699. struct em28xx *dev = *devhandle;
  1700. int retval = -ENOMEM;
  1701. int errCode;
  1702. unsigned int maxh, maxw;
  1703. dev->udev = udev;
  1704. mutex_init(&dev->lock);
  1705. spin_lock_init(&dev->slock);
  1706. init_waitqueue_head(&dev->open);
  1707. init_waitqueue_head(&dev->wait_frame);
  1708. init_waitqueue_head(&dev->wait_stream);
  1709. dev->em28xx_write_regs = em28xx_write_regs;
  1710. dev->em28xx_read_reg = em28xx_read_reg;
  1711. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1712. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1713. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1714. dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
  1715. errCode = em28xx_read_reg(dev, CHIPID_REG);
  1716. if (errCode >= 0)
  1717. em28xx_info("em28xx chip ID = %d\n", errCode);
  1718. em28xx_pre_card_setup(dev);
  1719. errCode = em28xx_config(dev);
  1720. if (errCode) {
  1721. em28xx_errdev("error configuring device\n");
  1722. em28xx_devused &= ~(1<<dev->devno);
  1723. kfree(dev);
  1724. return -ENOMEM;
  1725. }
  1726. /* register i2c bus */
  1727. em28xx_i2c_register(dev);
  1728. /* Do board specific init and eeprom reading */
  1729. em28xx_card_setup(dev);
  1730. /* Configure audio */
  1731. em28xx_audio_analog_set(dev);
  1732. /* configure the device */
  1733. em28xx_config_i2c(dev);
  1734. /* set default norm */
  1735. dev->norm = em28xx_video_template.current_norm;
  1736. maxw = norm_maxw(dev);
  1737. maxh = norm_maxh(dev);
  1738. /* set default image size */
  1739. dev->width = maxw;
  1740. dev->height = maxh;
  1741. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1742. dev->field_size = dev->width * dev->height;
  1743. dev->frame_size =
  1744. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1745. dev->bytesperline = dev->width * 2;
  1746. dev->hscale = 0;
  1747. dev->vscale = 0;
  1748. dev->ctl_input = 2;
  1749. errCode = em28xx_config(dev);
  1750. list_add_tail(&dev->devlist, &em28xx_devlist);
  1751. /* allocate and fill video video_device struct */
  1752. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
  1753. VID_TYPE_CAPTURE, "video");
  1754. if (NULL == dev->vdev) {
  1755. em28xx_errdev("cannot allocate video_device.\n");
  1756. goto fail_unreg;
  1757. }
  1758. if (dev->tuner_type != TUNER_ABSENT)
  1759. dev->vdev->type |= VID_TYPE_TUNER;
  1760. /* register v4l2 video video_device */
  1761. retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1762. video_nr[dev->devno]);
  1763. if (retval) {
  1764. em28xx_errdev("unable to register video device (error=%i).\n",
  1765. retval);
  1766. goto fail_unreg;
  1767. }
  1768. /* Allocate and fill vbi video_device struct */
  1769. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1770. VFL_TYPE_VBI, "vbi");
  1771. /* register v4l2 vbi video_device */
  1772. if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1773. vbi_nr[dev->devno]) < 0) {
  1774. em28xx_errdev("unable to register vbi device\n");
  1775. retval = -ENODEV;
  1776. goto fail_unreg;
  1777. }
  1778. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1779. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1780. VFL_TYPE_RADIO, "radio");
  1781. if (NULL == dev->radio_dev) {
  1782. em28xx_errdev("cannot allocate video_device.\n");
  1783. goto fail_unreg;
  1784. }
  1785. retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1786. radio_nr[dev->devno]);
  1787. if (retval < 0) {
  1788. em28xx_errdev("can't register radio device\n");
  1789. goto fail_unreg;
  1790. }
  1791. em28xx_info("Registered radio device as /dev/radio%d\n",
  1792. dev->radio_dev->minor & 0x1f);
  1793. }
  1794. /* init video dma queues */
  1795. INIT_LIST_HEAD(&dev->vidq.active);
  1796. INIT_LIST_HEAD(&dev->vidq.queued);
  1797. if (dev->has_msp34xx) {
  1798. /* Send a reset to other chips via gpio */
  1799. em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1800. msleep(3);
  1801. em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1802. msleep(3);
  1803. }
  1804. video_mux(dev, 0);
  1805. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1806. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1807. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1808. mutex_lock(&em28xx_extension_devlist_lock);
  1809. if (!list_empty(&em28xx_extension_devlist)) {
  1810. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1811. if (ops->id)
  1812. ops->init(dev);
  1813. }
  1814. }
  1815. mutex_unlock(&em28xx_extension_devlist_lock);
  1816. return 0;
  1817. fail_unreg:
  1818. em28xx_release_resources(dev);
  1819. mutex_unlock(&dev->lock);
  1820. kfree(dev);
  1821. return retval;
  1822. }
  1823. #if defined(CONFIG_MODULES) && defined(MODULE)
  1824. static void request_module_async(struct work_struct *work)
  1825. {
  1826. struct em28xx *dev = container_of(work,
  1827. struct em28xx, request_module_wk);
  1828. if (dev->has_audio_class)
  1829. request_module("snd-usb-audio");
  1830. else
  1831. request_module("em28xx-alsa");
  1832. }
  1833. static void request_modules(struct em28xx *dev)
  1834. {
  1835. INIT_WORK(&dev->request_module_wk, request_module_async);
  1836. schedule_work(&dev->request_module_wk);
  1837. }
  1838. #else
  1839. #define request_modules(dev)
  1840. #endif /* CONFIG_MODULES */
  1841. /*
  1842. * em28xx_usb_probe()
  1843. * checks for supported devices
  1844. */
  1845. static int em28xx_usb_probe(struct usb_interface *interface,
  1846. const struct usb_device_id *id)
  1847. {
  1848. const struct usb_endpoint_descriptor *endpoint;
  1849. struct usb_device *udev;
  1850. struct usb_interface *uif;
  1851. struct em28xx *dev = NULL;
  1852. int retval = -ENODEV;
  1853. int i, nr, ifnum;
  1854. udev = usb_get_dev(interface_to_usbdev(interface));
  1855. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1856. /* Check to see next free device and mark as used */
  1857. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1858. em28xx_devused|=1<<nr;
  1859. /* Don't register audio interfaces */
  1860. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1861. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1862. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1863. ifnum,
  1864. interface->altsetting[0].desc.bInterfaceClass);
  1865. em28xx_devused&=~(1<<nr);
  1866. return -ENODEV;
  1867. }
  1868. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1869. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1870. ifnum,
  1871. interface->altsetting[0].desc.bInterfaceClass);
  1872. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1873. /* check if the device has the iso in endpoint at the correct place */
  1874. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1875. USB_ENDPOINT_XFER_ISOC) {
  1876. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1877. em28xx_devused&=~(1<<nr);
  1878. return -ENODEV;
  1879. }
  1880. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1881. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1882. em28xx_devused&=~(1<<nr);
  1883. return -ENODEV;
  1884. }
  1885. if (nr >= EM28XX_MAXBOARDS) {
  1886. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1887. em28xx_devused&=~(1<<nr);
  1888. return -ENOMEM;
  1889. }
  1890. /* allocate memory for our device state and initialize it */
  1891. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1892. if (dev == NULL) {
  1893. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1894. em28xx_devused&=~(1<<nr);
  1895. return -ENOMEM;
  1896. }
  1897. snprintf(dev->name, 29, "em28xx #%d", nr);
  1898. dev->devno = nr;
  1899. dev->model = id->driver_info;
  1900. dev->alt = -1;
  1901. /* Checks if audio is provided by some interface */
  1902. for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
  1903. uif = udev->config->interface[i];
  1904. if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1905. dev->has_audio_class = 1;
  1906. break;
  1907. }
  1908. }
  1909. printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
  1910. dev->has_audio_class ? "Has" : "Doesn't have");
  1911. /* compute alternate max packet sizes */
  1912. uif = udev->actconfig->interface[0];
  1913. dev->num_alt=uif->num_altsetting;
  1914. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1915. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1916. dev->alt_max_pkt_size = kmalloc(32*
  1917. dev->num_alt,GFP_KERNEL);
  1918. if (dev->alt_max_pkt_size == NULL) {
  1919. em28xx_errdev("out of memory!\n");
  1920. em28xx_devused&=~(1<<nr);
  1921. kfree(dev);
  1922. return -ENOMEM;
  1923. }
  1924. for (i = 0; i < dev->num_alt ; i++) {
  1925. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1926. wMaxPacketSize);
  1927. dev->alt_max_pkt_size[i] =
  1928. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1929. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1930. dev->alt_max_pkt_size[i]);
  1931. }
  1932. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1933. dev->model = card[nr];
  1934. /* allocate device struct */
  1935. retval = em28xx_init_dev(&dev, udev, nr);
  1936. if (retval)
  1937. return retval;
  1938. em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
  1939. /* save our data pointer in this interface device */
  1940. usb_set_intfdata(interface, dev);
  1941. request_modules(dev);
  1942. return 0;
  1943. }
  1944. /*
  1945. * em28xx_usb_disconnect()
  1946. * called when the device gets diconencted
  1947. * video device will be unregistered on v4l2_close in case it is still open
  1948. */
  1949. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1950. {
  1951. struct em28xx *dev;
  1952. struct em28xx_ops *ops = NULL;
  1953. dev = usb_get_intfdata(interface);
  1954. usb_set_intfdata(interface, NULL);
  1955. if (!dev)
  1956. return;
  1957. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1958. /* wait until all current v4l2 io is finished then deallocate resources */
  1959. mutex_lock(&dev->lock);
  1960. wake_up_interruptible_all(&dev->open);
  1961. if (dev->users) {
  1962. em28xx_warn
  1963. ("device /dev/video%d is open! Deregistration and memory "
  1964. "deallocation are deferred on close.\n",
  1965. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1966. dev->state |= DEV_MISCONFIGURED;
  1967. em28xx_uninit_isoc(dev);
  1968. dev->state |= DEV_DISCONNECTED;
  1969. wake_up_interruptible(&dev->wait_frame);
  1970. wake_up_interruptible(&dev->wait_stream);
  1971. } else {
  1972. dev->state |= DEV_DISCONNECTED;
  1973. em28xx_release_resources(dev);
  1974. }
  1975. mutex_unlock(&dev->lock);
  1976. mutex_lock(&em28xx_extension_devlist_lock);
  1977. if (!list_empty(&em28xx_extension_devlist)) {
  1978. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1979. ops->fini(dev);
  1980. }
  1981. }
  1982. mutex_unlock(&em28xx_extension_devlist_lock);
  1983. if (!dev->users) {
  1984. kfree(dev->alt_max_pkt_size);
  1985. kfree(dev);
  1986. }
  1987. }
  1988. static struct usb_driver em28xx_usb_driver = {
  1989. .name = "em28xx",
  1990. .probe = em28xx_usb_probe,
  1991. .disconnect = em28xx_usb_disconnect,
  1992. .id_table = em28xx_id_table,
  1993. };
  1994. static int __init em28xx_module_init(void)
  1995. {
  1996. int result;
  1997. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1998. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1999. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2000. #ifdef SNAPSHOT
  2001. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  2002. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  2003. #endif
  2004. /* register this driver with the USB subsystem */
  2005. result = usb_register(&em28xx_usb_driver);
  2006. if (result)
  2007. em28xx_err(DRIVER_NAME
  2008. " usb_register failed. Error number %d.\n", result);
  2009. return result;
  2010. }
  2011. static void __exit em28xx_module_exit(void)
  2012. {
  2013. /* deregister this driver with the USB subsystem */
  2014. usb_deregister(&em28xx_usb_driver);
  2015. }
  2016. module_init(em28xx_module_init);
  2017. module_exit(em28xx_module_exit);