em28xx-video.c 59 KB

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