em28xx-video.c 55 KB

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