em28xx-video.c 59 KB

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