em28xx-video.c 60 KB

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