em28xx-video.c 60 KB

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