em28xx-video.c 56 KB

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