em28xx-video.c 60 KB

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