em28xx-video.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  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. mutex_lock(&dev->lock);
  644. if (dev->stream_on)
  645. rc = -EINVAL;
  646. else {
  647. dev->stream_on = 1;
  648. fh->stream_on = 1;
  649. }
  650. mutex_unlock(&dev->lock);
  651. return rc;
  652. }
  653. static int res_check(struct em28xx_fh *fh)
  654. {
  655. return (fh->stream_on);
  656. }
  657. static void res_free(struct em28xx_fh *fh)
  658. {
  659. struct em28xx *dev = fh->dev;
  660. mutex_lock(&dev->lock);
  661. fh->stream_on = 0;
  662. dev->stream_on = 0;
  663. mutex_unlock(&dev->lock);
  664. }
  665. /*
  666. * em28xx_get_ctrl()
  667. * return the current saturation, brightness or contrast, mute state
  668. */
  669. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  670. {
  671. switch (ctrl->id) {
  672. case V4L2_CID_AUDIO_MUTE:
  673. ctrl->value = dev->mute;
  674. return 0;
  675. case V4L2_CID_AUDIO_VOLUME:
  676. ctrl->value = dev->volume;
  677. return 0;
  678. default:
  679. return -EINVAL;
  680. }
  681. }
  682. /*
  683. * em28xx_set_ctrl()
  684. * mute or set new saturation, brightness or contrast
  685. */
  686. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  687. {
  688. switch (ctrl->id) {
  689. case V4L2_CID_AUDIO_MUTE:
  690. if (ctrl->value != dev->mute) {
  691. dev->mute = ctrl->value;
  692. return em28xx_audio_analog_set(dev);
  693. }
  694. return 0;
  695. case V4L2_CID_AUDIO_VOLUME:
  696. dev->volume = ctrl->value;
  697. return em28xx_audio_analog_set(dev);
  698. default:
  699. return -EINVAL;
  700. }
  701. }
  702. static int check_dev(struct em28xx *dev)
  703. {
  704. if (dev->state & DEV_DISCONNECTED) {
  705. em28xx_errdev("v4l2 ioctl: device not present\n");
  706. return -ENODEV;
  707. }
  708. if (dev->state & DEV_MISCONFIGURED) {
  709. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  710. "close and open it again\n");
  711. return -EIO;
  712. }
  713. return 0;
  714. }
  715. static void get_scale(struct em28xx *dev,
  716. unsigned int width, unsigned int height,
  717. unsigned int *hscale, unsigned int *vscale)
  718. {
  719. unsigned int maxw = norm_maxw(dev);
  720. unsigned int maxh = norm_maxh(dev);
  721. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  722. if (*hscale >= 0x4000)
  723. *hscale = 0x3fff;
  724. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  725. if (*vscale >= 0x4000)
  726. *vscale = 0x3fff;
  727. }
  728. /* ------------------------------------------------------------------
  729. IOCTL vidioc handling
  730. ------------------------------------------------------------------*/
  731. static int vidioc_g_fmt_cap(struct file *file, void *priv,
  732. struct v4l2_format *f)
  733. {
  734. struct em28xx_fh *fh = priv;
  735. struct em28xx *dev = fh->dev;
  736. mutex_lock(&dev->lock);
  737. f->fmt.pix.width = dev->width;
  738. f->fmt.pix.height = dev->height;
  739. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  740. f->fmt.pix.bytesperline = dev->bytesperline;
  741. f->fmt.pix.sizeimage = dev->frame_size;
  742. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  743. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  744. f->fmt.pix.field = dev->interlaced ?
  745. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  746. mutex_unlock(&dev->lock);
  747. return 0;
  748. }
  749. static int vidioc_try_fmt_cap(struct file *file, void *priv,
  750. struct v4l2_format *f)
  751. {
  752. struct em28xx_fh *fh = priv;
  753. struct em28xx *dev = fh->dev;
  754. int width = f->fmt.pix.width;
  755. int height = f->fmt.pix.height;
  756. unsigned int maxw = norm_maxw(dev);
  757. unsigned int maxh = norm_maxh(dev);
  758. unsigned int hscale, vscale;
  759. /* width must even because of the YUYV format
  760. height must be even because of interlacing */
  761. height &= 0xfffe;
  762. width &= 0xfffe;
  763. if (height < 32)
  764. height = 32;
  765. if (height > maxh)
  766. height = maxh;
  767. if (width < 48)
  768. width = 48;
  769. if (width > maxw)
  770. width = maxw;
  771. mutex_lock(&dev->lock);
  772. if (dev->is_em2800) {
  773. /* the em2800 can only scale down to 50% */
  774. if (height % (maxh / 2))
  775. height = maxh;
  776. if (width % (maxw / 2))
  777. width = maxw;
  778. /* according to empiatech support */
  779. /* the MaxPacketSize is to small to support */
  780. /* framesizes larger than 640x480 @ 30 fps */
  781. /* or 640x576 @ 25 fps. As this would cut */
  782. /* of a part of the image we prefer */
  783. /* 360x576 or 360x480 for now */
  784. if (width == maxw && height == maxh)
  785. width /= 2;
  786. }
  787. get_scale(dev, width, height, &hscale, &vscale);
  788. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  789. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  790. f->fmt.pix.width = width;
  791. f->fmt.pix.height = height;
  792. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  793. f->fmt.pix.bytesperline = width * 2;
  794. f->fmt.pix.sizeimage = width * 2 * height;
  795. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  796. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  797. mutex_unlock(&dev->lock);
  798. return 0;
  799. }
  800. static int vidioc_s_fmt_cap(struct file *file, void *priv,
  801. struct v4l2_format *f)
  802. {
  803. struct em28xx_fh *fh = priv;
  804. struct em28xx *dev = fh->dev;
  805. int rc;
  806. rc = check_dev(dev);
  807. if (rc < 0)
  808. return rc;
  809. vidioc_try_fmt_cap(file, priv, f);
  810. mutex_lock(&dev->lock);
  811. /* set new image size */
  812. dev->width = f->fmt.pix.width;
  813. dev->height = f->fmt.pix.height;
  814. dev->frame_size = dev->width * dev->height * 2;
  815. dev->field_size = dev->frame_size >> 1;
  816. dev->bytesperline = dev->width * 2;
  817. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  818. em28xx_set_alternate(dev);
  819. em28xx_resolution_set(dev);
  820. mutex_unlock(&dev->lock);
  821. return 0;
  822. }
  823. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  824. {
  825. struct em28xx_fh *fh = priv;
  826. struct em28xx *dev = fh->dev;
  827. struct v4l2_format f;
  828. int rc;
  829. rc = check_dev(dev);
  830. if (rc < 0)
  831. return rc;
  832. mutex_lock(&dev->lock);
  833. dev->norm = *norm;
  834. mutex_unlock(&dev->lock);
  835. /* Adjusts width/height, if needed */
  836. f.fmt.pix.width = dev->width;
  837. f.fmt.pix.height = dev->height;
  838. vidioc_try_fmt_cap(file, priv, &f);
  839. mutex_lock(&dev->lock);
  840. /* set new image size */
  841. dev->width = f.fmt.pix.width;
  842. dev->height = f.fmt.pix.height;
  843. dev->frame_size = dev->width * dev->height * 2;
  844. dev->field_size = dev->frame_size >> 1;
  845. dev->bytesperline = dev->width * 2;
  846. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  847. em28xx_resolution_set(dev);
  848. em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
  849. mutex_unlock(&dev->lock);
  850. return 0;
  851. }
  852. static const char *iname[] = {
  853. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  854. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  855. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  856. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  857. [EM28XX_VMUX_SVIDEO] = "S-Video",
  858. [EM28XX_VMUX_TELEVISION] = "Television",
  859. [EM28XX_VMUX_CABLE] = "Cable TV",
  860. [EM28XX_VMUX_DVB] = "DVB",
  861. [EM28XX_VMUX_DEBUG] = "for debug only",
  862. };
  863. static int vidioc_enum_input(struct file *file, void *priv,
  864. struct v4l2_input *i)
  865. {
  866. struct em28xx_fh *fh = priv;
  867. struct em28xx *dev = fh->dev;
  868. unsigned int n;
  869. n = i->index;
  870. if (n >= MAX_EM28XX_INPUT)
  871. return -EINVAL;
  872. if (0 == INPUT(n)->type)
  873. return -EINVAL;
  874. i->index = n;
  875. i->type = V4L2_INPUT_TYPE_CAMERA;
  876. strcpy(i->name, iname[INPUT(n)->type]);
  877. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  878. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  879. i->type = V4L2_INPUT_TYPE_TUNER;
  880. i->std = dev->vdev->tvnorms;
  881. return 0;
  882. }
  883. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  884. {
  885. struct em28xx_fh *fh = priv;
  886. struct em28xx *dev = fh->dev;
  887. *i = dev->ctl_input;
  888. return 0;
  889. }
  890. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  891. {
  892. struct em28xx_fh *fh = priv;
  893. struct em28xx *dev = fh->dev;
  894. int rc;
  895. rc = check_dev(dev);
  896. if (rc < 0)
  897. return rc;
  898. if (i >= MAX_EM28XX_INPUT)
  899. return -EINVAL;
  900. if (0 == INPUT(i)->type)
  901. return -EINVAL;
  902. mutex_lock(&dev->lock);
  903. video_mux(dev, i);
  904. mutex_unlock(&dev->lock);
  905. return 0;
  906. }
  907. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  908. {
  909. struct em28xx_fh *fh = priv;
  910. struct em28xx *dev = fh->dev;
  911. unsigned int index = a->index;
  912. if (a->index > 1)
  913. return -EINVAL;
  914. index = dev->ctl_ainput;
  915. if (index == 0) {
  916. strcpy(a->name, "Television");
  917. } else {
  918. strcpy(a->name, "Line In");
  919. }
  920. a->capability = V4L2_AUDCAP_STEREO;
  921. a->index = index;
  922. return 0;
  923. }
  924. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  925. {
  926. struct em28xx_fh *fh = priv;
  927. struct em28xx *dev = fh->dev;
  928. if (a->index != dev->ctl_ainput)
  929. return -EINVAL;
  930. return 0;
  931. }
  932. static int vidioc_queryctrl(struct file *file, void *priv,
  933. struct v4l2_queryctrl *qc)
  934. {
  935. struct em28xx_fh *fh = priv;
  936. struct em28xx *dev = fh->dev;
  937. int id = qc->id;
  938. int i;
  939. int rc;
  940. rc = check_dev(dev);
  941. if (rc < 0)
  942. return rc;
  943. memset(qc, 0, sizeof(*qc));
  944. qc->id = id;
  945. if (!dev->has_msp34xx) {
  946. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  947. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  948. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  949. return 0;
  950. }
  951. }
  952. }
  953. mutex_lock(&dev->lock);
  954. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  955. mutex_unlock(&dev->lock);
  956. if (qc->type)
  957. return 0;
  958. else
  959. return -EINVAL;
  960. }
  961. static int vidioc_g_ctrl(struct file *file, void *priv,
  962. struct v4l2_control *ctrl)
  963. {
  964. struct em28xx_fh *fh = priv;
  965. struct em28xx *dev = fh->dev;
  966. int rc;
  967. rc = check_dev(dev);
  968. if (rc < 0)
  969. return rc;
  970. mutex_lock(&dev->lock);
  971. if (!dev->has_msp34xx)
  972. rc = em28xx_get_ctrl(dev, ctrl);
  973. else
  974. rc = -EINVAL;
  975. if (rc == -EINVAL) {
  976. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  977. rc = 0;
  978. }
  979. mutex_unlock(&dev->lock);
  980. return rc;
  981. }
  982. static int vidioc_s_ctrl(struct file *file, void *priv,
  983. struct v4l2_control *ctrl)
  984. {
  985. struct em28xx_fh *fh = priv;
  986. struct em28xx *dev = fh->dev;
  987. u8 i;
  988. int rc;
  989. rc = check_dev(dev);
  990. if (rc < 0)
  991. return rc;
  992. mutex_lock(&dev->lock);
  993. if (dev->has_msp34xx)
  994. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  995. else {
  996. rc = 1;
  997. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  998. if (ctrl->id == em28xx_qctrl[i].id) {
  999. if (ctrl->value < em28xx_qctrl[i].minimum ||
  1000. ctrl->value > em28xx_qctrl[i].maximum) {
  1001. rc = -ERANGE;
  1002. break;
  1003. }
  1004. rc = em28xx_set_ctrl(dev, ctrl);
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. /* Control not found - try to send it to the attached devices */
  1010. if (rc == 1) {
  1011. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  1012. rc = 0;
  1013. }
  1014. mutex_unlock(&dev->lock);
  1015. return rc;
  1016. }
  1017. static int vidioc_g_tuner(struct file *file, void *priv,
  1018. struct v4l2_tuner *t)
  1019. {
  1020. struct em28xx_fh *fh = priv;
  1021. struct em28xx *dev = fh->dev;
  1022. int rc;
  1023. rc = check_dev(dev);
  1024. if (rc < 0)
  1025. return rc;
  1026. if (0 != t->index)
  1027. return -EINVAL;
  1028. strcpy(t->name, "Tuner");
  1029. mutex_lock(&dev->lock);
  1030. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1031. mutex_unlock(&dev->lock);
  1032. return 0;
  1033. }
  1034. static int vidioc_s_tuner(struct file *file, void *priv,
  1035. struct v4l2_tuner *t)
  1036. {
  1037. struct em28xx_fh *fh = priv;
  1038. struct em28xx *dev = fh->dev;
  1039. int rc;
  1040. rc = check_dev(dev);
  1041. if (rc < 0)
  1042. return rc;
  1043. if (0 != t->index)
  1044. return -EINVAL;
  1045. mutex_lock(&dev->lock);
  1046. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1047. mutex_unlock(&dev->lock);
  1048. return 0;
  1049. }
  1050. static int vidioc_g_frequency(struct file *file, void *priv,
  1051. struct v4l2_frequency *f)
  1052. {
  1053. struct em28xx_fh *fh = priv;
  1054. struct em28xx *dev = fh->dev;
  1055. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1056. f->frequency = dev->ctl_freq;
  1057. return 0;
  1058. }
  1059. static int vidioc_s_frequency(struct file *file, void *priv,
  1060. struct v4l2_frequency *f)
  1061. {
  1062. struct em28xx_fh *fh = priv;
  1063. struct em28xx *dev = fh->dev;
  1064. int rc;
  1065. rc = check_dev(dev);
  1066. if (rc < 0)
  1067. return rc;
  1068. if (0 != f->tuner)
  1069. return -EINVAL;
  1070. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1071. return -EINVAL;
  1072. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1073. return -EINVAL;
  1074. mutex_lock(&dev->lock);
  1075. dev->ctl_freq = f->frequency;
  1076. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  1077. mutex_unlock(&dev->lock);
  1078. return 0;
  1079. }
  1080. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1081. static int em28xx_reg_len(int reg)
  1082. {
  1083. switch (reg) {
  1084. case AC97LSB_REG:
  1085. case HSCALELOW_REG:
  1086. case VSCALELOW_REG:
  1087. return 2;
  1088. default:
  1089. return 1;
  1090. }
  1091. }
  1092. static int vidioc_g_register(struct file *file, void *priv,
  1093. struct v4l2_register *reg)
  1094. {
  1095. struct em28xx_fh *fh = priv;
  1096. struct em28xx *dev = fh->dev;
  1097. int ret;
  1098. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1099. return -EINVAL;
  1100. if (em28xx_reg_len(reg->reg) == 1) {
  1101. ret = em28xx_read_reg(dev, reg->reg);
  1102. if (ret < 0)
  1103. return ret;
  1104. reg->val = ret;
  1105. } else {
  1106. u64 val = 0;
  1107. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1108. reg->reg, (char *)&val, 2);
  1109. if (ret < 0)
  1110. return ret;
  1111. reg->val = cpu_to_le64((__u64)val);
  1112. }
  1113. return 0;
  1114. }
  1115. static int vidioc_s_register(struct file *file, void *priv,
  1116. struct v4l2_register *reg)
  1117. {
  1118. struct em28xx_fh *fh = priv;
  1119. struct em28xx *dev = fh->dev;
  1120. u64 buf;
  1121. buf = le64_to_cpu((__u64)reg->val);
  1122. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1123. em28xx_reg_len(reg->reg));
  1124. }
  1125. #endif
  1126. static int vidioc_cropcap(struct file *file, void *priv,
  1127. struct v4l2_cropcap *cc)
  1128. {
  1129. struct em28xx_fh *fh = priv;
  1130. struct em28xx *dev = fh->dev;
  1131. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1132. return -EINVAL;
  1133. cc->bounds.left = 0;
  1134. cc->bounds.top = 0;
  1135. cc->bounds.width = dev->width;
  1136. cc->bounds.height = dev->height;
  1137. cc->defrect = cc->bounds;
  1138. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1139. cc->pixelaspect.denominator = 59;
  1140. return 0;
  1141. }
  1142. static int vidioc_streamon(struct file *file, void *priv,
  1143. enum v4l2_buf_type type)
  1144. {
  1145. struct em28xx_fh *fh = priv;
  1146. struct em28xx *dev = fh->dev;
  1147. int rc;
  1148. rc = check_dev(dev);
  1149. if (rc < 0)
  1150. return rc;
  1151. if (unlikely(res_get(fh) < 0))
  1152. return -EBUSY;
  1153. return (videobuf_streamon(&fh->vb_vidq));
  1154. }
  1155. static int vidioc_streamoff(struct file *file, void *priv,
  1156. enum v4l2_buf_type type)
  1157. {
  1158. struct em28xx_fh *fh = priv;
  1159. struct em28xx *dev = fh->dev;
  1160. int rc;
  1161. rc = check_dev(dev);
  1162. if (rc < 0)
  1163. return rc;
  1164. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1165. return -EINVAL;
  1166. if (type != fh->type)
  1167. return -EINVAL;
  1168. videobuf_streamoff(&fh->vb_vidq);
  1169. res_free(fh);
  1170. return 0;
  1171. }
  1172. static int vidioc_querycap(struct file *file, void *priv,
  1173. struct v4l2_capability *cap)
  1174. {
  1175. struct em28xx_fh *fh = priv;
  1176. struct em28xx *dev = fh->dev;
  1177. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1178. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1179. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1180. cap->version = EM28XX_VERSION_CODE;
  1181. cap->capabilities =
  1182. V4L2_CAP_SLICED_VBI_CAPTURE |
  1183. V4L2_CAP_VIDEO_CAPTURE |
  1184. V4L2_CAP_AUDIO |
  1185. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1186. if (dev->tuner_type != TUNER_ABSENT)
  1187. cap->capabilities |= V4L2_CAP_TUNER;
  1188. return 0;
  1189. }
  1190. static int vidioc_enum_fmt_cap(struct file *file, void *priv,
  1191. struct v4l2_fmtdesc *fmtd)
  1192. {
  1193. if (fmtd->index != 0)
  1194. return -EINVAL;
  1195. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1196. strcpy(fmtd->description, "Packed YUY2");
  1197. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1198. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1199. return 0;
  1200. }
  1201. /* Sliced VBI ioctls */
  1202. static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
  1203. struct v4l2_format *f)
  1204. {
  1205. struct em28xx_fh *fh = priv;
  1206. struct em28xx *dev = fh->dev;
  1207. int rc;
  1208. rc = check_dev(dev);
  1209. if (rc < 0)
  1210. return rc;
  1211. mutex_lock(&dev->lock);
  1212. f->fmt.sliced.service_set = 0;
  1213. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1214. if (f->fmt.sliced.service_set == 0)
  1215. rc = -EINVAL;
  1216. mutex_unlock(&dev->lock);
  1217. return rc;
  1218. }
  1219. static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
  1220. struct v4l2_format *f)
  1221. {
  1222. struct em28xx_fh *fh = priv;
  1223. struct em28xx *dev = fh->dev;
  1224. int rc;
  1225. rc = check_dev(dev);
  1226. if (rc < 0)
  1227. return rc;
  1228. mutex_lock(&dev->lock);
  1229. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1230. mutex_unlock(&dev->lock);
  1231. if (f->fmt.sliced.service_set == 0)
  1232. return -EINVAL;
  1233. return 0;
  1234. }
  1235. static int vidioc_reqbufs(struct file *file, void *priv,
  1236. struct v4l2_requestbuffers *rb)
  1237. {
  1238. struct em28xx_fh *fh = priv;
  1239. struct em28xx *dev = fh->dev;
  1240. int rc;
  1241. rc = check_dev(dev);
  1242. if (rc < 0)
  1243. return rc;
  1244. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1245. }
  1246. static int vidioc_querybuf(struct file *file, void *priv,
  1247. struct v4l2_buffer *b)
  1248. {
  1249. struct em28xx_fh *fh = priv;
  1250. struct em28xx *dev = fh->dev;
  1251. int rc;
  1252. rc = check_dev(dev);
  1253. if (rc < 0)
  1254. return rc;
  1255. return (videobuf_querybuf(&fh->vb_vidq, b));
  1256. }
  1257. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1258. {
  1259. struct em28xx_fh *fh = priv;
  1260. struct em28xx *dev = fh->dev;
  1261. int rc;
  1262. rc = check_dev(dev);
  1263. if (rc < 0)
  1264. return rc;
  1265. return (videobuf_qbuf(&fh->vb_vidq, b));
  1266. }
  1267. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1268. {
  1269. struct em28xx_fh *fh = priv;
  1270. struct em28xx *dev = fh->dev;
  1271. int rc;
  1272. rc = check_dev(dev);
  1273. if (rc < 0)
  1274. return rc;
  1275. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1276. file->f_flags & O_NONBLOCK));
  1277. }
  1278. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1279. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1280. {
  1281. struct em28xx_fh *fh = priv;
  1282. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1283. }
  1284. #endif
  1285. /* ----------------------------------------------------------- */
  1286. /* RADIO ESPECIFIC IOCTLS */
  1287. /* ----------------------------------------------------------- */
  1288. static int radio_querycap(struct file *file, void *priv,
  1289. struct v4l2_capability *cap)
  1290. {
  1291. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1292. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1293. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1294. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1295. cap->version = EM28XX_VERSION_CODE;
  1296. cap->capabilities = V4L2_CAP_TUNER;
  1297. return 0;
  1298. }
  1299. static int radio_g_tuner(struct file *file, void *priv,
  1300. struct v4l2_tuner *t)
  1301. {
  1302. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1303. if (unlikely(t->index > 0))
  1304. return -EINVAL;
  1305. strcpy(t->name, "Radio");
  1306. t->type = V4L2_TUNER_RADIO;
  1307. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1308. return 0;
  1309. }
  1310. static int radio_enum_input(struct file *file, void *priv,
  1311. struct v4l2_input *i)
  1312. {
  1313. if (i->index != 0)
  1314. return -EINVAL;
  1315. strcpy(i->name, "Radio");
  1316. i->type = V4L2_INPUT_TYPE_TUNER;
  1317. return 0;
  1318. }
  1319. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1320. {
  1321. if (unlikely(a->index))
  1322. return -EINVAL;
  1323. strcpy(a->name, "Radio");
  1324. return 0;
  1325. }
  1326. static int radio_s_tuner(struct file *file, void *priv,
  1327. struct v4l2_tuner *t)
  1328. {
  1329. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1330. if (0 != t->index)
  1331. return -EINVAL;
  1332. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1333. return 0;
  1334. }
  1335. static int radio_s_audio(struct file *file, void *fh,
  1336. struct v4l2_audio *a)
  1337. {
  1338. return 0;
  1339. }
  1340. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1341. {
  1342. return 0;
  1343. }
  1344. static int radio_queryctrl(struct file *file, void *priv,
  1345. struct v4l2_queryctrl *qc)
  1346. {
  1347. int i;
  1348. if (qc->id < V4L2_CID_BASE ||
  1349. qc->id >= V4L2_CID_LASTP1)
  1350. return -EINVAL;
  1351. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1352. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1353. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1354. return 0;
  1355. }
  1356. }
  1357. return -EINVAL;
  1358. }
  1359. /*
  1360. * em28xx_v4l2_open()
  1361. * inits the device and starts isoc transfer
  1362. */
  1363. static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
  1364. {
  1365. int minor = iminor(inode);
  1366. int errCode = 0, radio = 0;
  1367. struct em28xx *h,*dev = NULL;
  1368. struct em28xx_fh *fh;
  1369. enum v4l2_buf_type fh_type = 0;
  1370. list_for_each_entry(h, &em28xx_devlist, devlist) {
  1371. if (h->vdev->minor == minor) {
  1372. dev = h;
  1373. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1374. }
  1375. if (h->vbi_dev->minor == minor) {
  1376. dev = h;
  1377. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1378. }
  1379. if (h->radio_dev &&
  1380. h->radio_dev->minor == minor) {
  1381. radio = 1;
  1382. dev = h;
  1383. }
  1384. }
  1385. if (NULL == dev)
  1386. return -ENODEV;
  1387. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1388. minor, v4l2_type_names[fh_type], dev->users);
  1389. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1390. if (!fh) {
  1391. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1392. return -ENOMEM;
  1393. }
  1394. mutex_lock(&dev->lock);
  1395. fh->dev = dev;
  1396. fh->radio = radio;
  1397. fh->type = fh_type;
  1398. filp->private_data = fh;
  1399. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1400. dev->width = norm_maxw(dev);
  1401. dev->height = norm_maxh(dev);
  1402. dev->frame_size = dev->width * dev->height * 2;
  1403. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  1404. dev->bytesperline = dev->width * 2;
  1405. dev->hscale = 0;
  1406. dev->vscale = 0;
  1407. em28xx_set_alternate(dev);
  1408. em28xx_resolution_set(dev);
  1409. }
  1410. if (fh->radio) {
  1411. em28xx_videodbg("video_open: setting radio device\n");
  1412. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1413. }
  1414. dev->users++;
  1415. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1416. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1417. sizeof(struct em28xx_buffer), fh);
  1418. mutex_unlock(&dev->lock);
  1419. return errCode;
  1420. }
  1421. /*
  1422. * em28xx_realease_resources()
  1423. * unregisters the v4l2,i2c and usb devices
  1424. * called when the device gets disconected or at module unload
  1425. */
  1426. static void em28xx_release_resources(struct em28xx *dev)
  1427. {
  1428. /*FIXME: I2C IR should be disconnected */
  1429. em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
  1430. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1431. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1432. list_del(&dev->devlist);
  1433. if (dev->radio_dev) {
  1434. if (-1 != dev->radio_dev->minor)
  1435. video_unregister_device(dev->radio_dev);
  1436. else
  1437. video_device_release(dev->radio_dev);
  1438. dev->radio_dev = NULL;
  1439. }
  1440. if (dev->vbi_dev) {
  1441. if (-1 != dev->vbi_dev->minor)
  1442. video_unregister_device(dev->vbi_dev);
  1443. else
  1444. video_device_release(dev->vbi_dev);
  1445. dev->vbi_dev = NULL;
  1446. }
  1447. if (dev->vdev) {
  1448. if (-1 != dev->vdev->minor)
  1449. video_unregister_device(dev->vdev);
  1450. else
  1451. video_device_release(dev->vdev);
  1452. dev->vdev = NULL;
  1453. }
  1454. em28xx_i2c_unregister(dev);
  1455. usb_put_dev(dev->udev);
  1456. /* Mark device as unused */
  1457. em28xx_devused&=~(1<<dev->devno);
  1458. }
  1459. /*
  1460. * em28xx_v4l2_close()
  1461. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  1462. */
  1463. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  1464. {
  1465. struct em28xx_fh *fh = filp->private_data;
  1466. struct em28xx *dev = fh->dev;
  1467. int errCode;
  1468. em28xx_videodbg("users=%d\n", dev->users);
  1469. if (res_check(fh))
  1470. res_free(fh);
  1471. mutex_lock(&dev->lock);
  1472. if (dev->users == 1) {
  1473. videobuf_stop(&fh->vb_vidq);
  1474. videobuf_mmap_free(&fh->vb_vidq);
  1475. /* the device is already disconnect,
  1476. free the remaining resources */
  1477. if (dev->state & DEV_DISCONNECTED) {
  1478. em28xx_release_resources(dev);
  1479. mutex_unlock(&dev->lock);
  1480. kfree(dev);
  1481. return 0;
  1482. }
  1483. /* do this before setting alternate! */
  1484. em28xx_uninit_isoc(dev);
  1485. /* set alternate 0 */
  1486. dev->alt = 0;
  1487. em28xx_videodbg("setting alternate 0\n");
  1488. errCode = usb_set_interface(dev->udev, 0, 0);
  1489. if (errCode < 0) {
  1490. em28xx_errdev("cannot change alternate number to "
  1491. "0 (error=%i)\n", errCode);
  1492. }
  1493. }
  1494. kfree(fh);
  1495. dev->users--;
  1496. wake_up_interruptible_nr(&dev->open, 1);
  1497. mutex_unlock(&dev->lock);
  1498. return 0;
  1499. }
  1500. /*
  1501. * em28xx_v4l2_read()
  1502. * will allocate buffers when called for the first time
  1503. */
  1504. static ssize_t
  1505. em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
  1506. loff_t *pos)
  1507. {
  1508. struct em28xx_fh *fh = filp->private_data;
  1509. struct em28xx *dev = fh->dev;
  1510. int rc;
  1511. rc = check_dev(dev);
  1512. if (rc < 0)
  1513. return rc;
  1514. /* FIXME: read() is not prepared to allow changing the video
  1515. resolution while streaming. Seems a bug at em28xx_set_fmt
  1516. */
  1517. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1518. if (unlikely(res_get(fh)))
  1519. return -EBUSY;
  1520. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1521. filp->f_flags & O_NONBLOCK);
  1522. }
  1523. return 0;
  1524. }
  1525. /*
  1526. * em28xx_v4l2_poll()
  1527. * will allocate buffers when called for the first time
  1528. */
  1529. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1530. {
  1531. struct em28xx_fh *fh = filp->private_data;
  1532. struct em28xx *dev = fh->dev;
  1533. int rc;
  1534. rc = check_dev(dev);
  1535. if (rc < 0)
  1536. return rc;
  1537. if (unlikely(res_get(fh) < 0))
  1538. return POLLERR;
  1539. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1540. return POLLERR;
  1541. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1542. }
  1543. /*
  1544. * em28xx_v4l2_mmap()
  1545. */
  1546. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1547. {
  1548. struct em28xx_fh *fh = filp->private_data;
  1549. struct em28xx *dev = fh->dev;
  1550. int rc;
  1551. if (unlikely(res_get(fh) < 0))
  1552. return -EBUSY;
  1553. rc = check_dev(dev);
  1554. if (rc < 0)
  1555. return rc;
  1556. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1557. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1558. (unsigned long)vma->vm_start,
  1559. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1560. rc);
  1561. return rc;
  1562. }
  1563. static const struct file_operations em28xx_v4l_fops = {
  1564. .owner = THIS_MODULE,
  1565. .open = em28xx_v4l2_open,
  1566. .release = em28xx_v4l2_close,
  1567. .read = em28xx_v4l2_read,
  1568. .poll = em28xx_v4l2_poll,
  1569. .mmap = em28xx_v4l2_mmap,
  1570. .ioctl = video_ioctl2,
  1571. .llseek = no_llseek,
  1572. .compat_ioctl = v4l_compat_ioctl32,
  1573. };
  1574. static const struct file_operations radio_fops = {
  1575. .owner = THIS_MODULE,
  1576. .open = em28xx_v4l2_open,
  1577. .release = em28xx_v4l2_close,
  1578. .ioctl = video_ioctl2,
  1579. .compat_ioctl = v4l_compat_ioctl32,
  1580. .llseek = no_llseek,
  1581. };
  1582. static const struct video_device em28xx_video_template = {
  1583. .fops = &em28xx_v4l_fops,
  1584. .release = video_device_release,
  1585. .minor = -1,
  1586. .vidioc_querycap = vidioc_querycap,
  1587. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1588. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1589. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1590. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1591. .vidioc_g_audio = vidioc_g_audio,
  1592. .vidioc_s_audio = vidioc_s_audio,
  1593. .vidioc_cropcap = vidioc_cropcap,
  1594. .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
  1595. .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
  1596. .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
  1597. .vidioc_reqbufs = vidioc_reqbufs,
  1598. .vidioc_querybuf = vidioc_querybuf,
  1599. .vidioc_qbuf = vidioc_qbuf,
  1600. .vidioc_dqbuf = vidioc_dqbuf,
  1601. .vidioc_s_std = vidioc_s_std,
  1602. .vidioc_enum_input = vidioc_enum_input,
  1603. .vidioc_g_input = vidioc_g_input,
  1604. .vidioc_s_input = vidioc_s_input,
  1605. .vidioc_queryctrl = vidioc_queryctrl,
  1606. .vidioc_g_ctrl = vidioc_g_ctrl,
  1607. .vidioc_s_ctrl = vidioc_s_ctrl,
  1608. .vidioc_streamon = vidioc_streamon,
  1609. .vidioc_streamoff = vidioc_streamoff,
  1610. .vidioc_g_tuner = vidioc_g_tuner,
  1611. .vidioc_s_tuner = vidioc_s_tuner,
  1612. .vidioc_g_frequency = vidioc_g_frequency,
  1613. .vidioc_s_frequency = vidioc_s_frequency,
  1614. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1615. .vidioc_g_register = vidioc_g_register,
  1616. .vidioc_s_register = vidioc_s_register,
  1617. #endif
  1618. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1619. .vidiocgmbuf = vidiocgmbuf,
  1620. #endif
  1621. .tvnorms = V4L2_STD_ALL,
  1622. .current_norm = V4L2_STD_PAL,
  1623. };
  1624. static struct video_device em28xx_radio_template = {
  1625. .name = "em28xx-radio",
  1626. .type = VID_TYPE_TUNER,
  1627. .fops = &radio_fops,
  1628. .minor = -1,
  1629. .vidioc_querycap = radio_querycap,
  1630. .vidioc_g_tuner = radio_g_tuner,
  1631. .vidioc_enum_input = radio_enum_input,
  1632. .vidioc_g_audio = radio_g_audio,
  1633. .vidioc_s_tuner = radio_s_tuner,
  1634. .vidioc_s_audio = radio_s_audio,
  1635. .vidioc_s_input = radio_s_input,
  1636. .vidioc_queryctrl = radio_queryctrl,
  1637. .vidioc_g_ctrl = vidioc_g_ctrl,
  1638. .vidioc_s_ctrl = vidioc_s_ctrl,
  1639. .vidioc_g_frequency = vidioc_g_frequency,
  1640. .vidioc_s_frequency = vidioc_s_frequency,
  1641. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1642. .vidioc_g_register = vidioc_g_register,
  1643. .vidioc_s_register = vidioc_s_register,
  1644. #endif
  1645. };
  1646. /******************************** usb interface *****************************************/
  1647. static LIST_HEAD(em28xx_extension_devlist);
  1648. static DEFINE_MUTEX(em28xx_extension_devlist_lock);
  1649. int em28xx_register_extension(struct em28xx_ops *ops)
  1650. {
  1651. struct em28xx *h, *dev = NULL;
  1652. list_for_each_entry(h, &em28xx_devlist, devlist)
  1653. dev = h;
  1654. mutex_lock(&em28xx_extension_devlist_lock);
  1655. list_add_tail(&ops->next, &em28xx_extension_devlist);
  1656. if (dev)
  1657. ops->init(dev);
  1658. printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
  1659. mutex_unlock(&em28xx_extension_devlist_lock);
  1660. return 0;
  1661. }
  1662. EXPORT_SYMBOL(em28xx_register_extension);
  1663. void em28xx_unregister_extension(struct em28xx_ops *ops)
  1664. {
  1665. struct em28xx *h, *dev = NULL;
  1666. list_for_each_entry(h, &em28xx_devlist, devlist)
  1667. dev = h;
  1668. if (dev)
  1669. ops->fini(dev);
  1670. mutex_lock(&em28xx_extension_devlist_lock);
  1671. printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
  1672. list_del(&ops->next);
  1673. mutex_unlock(&em28xx_extension_devlist_lock);
  1674. }
  1675. EXPORT_SYMBOL(em28xx_unregister_extension);
  1676. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1677. const struct video_device *template,
  1678. const int type,
  1679. const char *type_name)
  1680. {
  1681. struct video_device *vfd;
  1682. vfd = video_device_alloc();
  1683. if (NULL == vfd)
  1684. return NULL;
  1685. *vfd = *template;
  1686. vfd->minor = -1;
  1687. vfd->dev = &dev->udev->dev;
  1688. vfd->release = video_device_release;
  1689. vfd->type = type;
  1690. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1691. dev->name, type_name);
  1692. return vfd;
  1693. }
  1694. /*
  1695. * em28xx_init_dev()
  1696. * allocates and inits the device structs, registers i2c bus and v4l device
  1697. */
  1698. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1699. int minor)
  1700. {
  1701. struct em28xx_ops *ops = NULL;
  1702. struct em28xx *dev = *devhandle;
  1703. int retval = -ENOMEM;
  1704. int errCode;
  1705. unsigned int maxh, maxw;
  1706. dev->udev = udev;
  1707. mutex_init(&dev->lock);
  1708. spin_lock_init(&dev->slock);
  1709. init_waitqueue_head(&dev->open);
  1710. init_waitqueue_head(&dev->wait_frame);
  1711. init_waitqueue_head(&dev->wait_stream);
  1712. dev->em28xx_write_regs = em28xx_write_regs;
  1713. dev->em28xx_read_reg = em28xx_read_reg;
  1714. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1715. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1716. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1717. dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
  1718. errCode = em28xx_read_reg(dev, CHIPID_REG);
  1719. if (errCode >= 0)
  1720. em28xx_info("em28xx chip ID = %d\n", errCode);
  1721. em28xx_pre_card_setup(dev);
  1722. errCode = em28xx_config(dev);
  1723. if (errCode) {
  1724. em28xx_errdev("error configuring device\n");
  1725. em28xx_devused &= ~(1<<dev->devno);
  1726. kfree(dev);
  1727. return -ENOMEM;
  1728. }
  1729. /* register i2c bus */
  1730. em28xx_i2c_register(dev);
  1731. /* Do board specific init and eeprom reading */
  1732. em28xx_card_setup(dev);
  1733. /* Configure audio */
  1734. em28xx_audio_analog_set(dev);
  1735. /* configure the device */
  1736. em28xx_config_i2c(dev);
  1737. /* set default norm */
  1738. dev->norm = em28xx_video_template.current_norm;
  1739. maxw = norm_maxw(dev);
  1740. maxh = norm_maxh(dev);
  1741. /* set default image size */
  1742. dev->width = maxw;
  1743. dev->height = maxh;
  1744. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1745. dev->field_size = dev->width * dev->height;
  1746. dev->frame_size =
  1747. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1748. dev->bytesperline = dev->width * 2;
  1749. dev->hscale = 0;
  1750. dev->vscale = 0;
  1751. dev->ctl_input = 2;
  1752. errCode = em28xx_config(dev);
  1753. list_add_tail(&dev->devlist, &em28xx_devlist);
  1754. /* allocate and fill video video_device struct */
  1755. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
  1756. VID_TYPE_CAPTURE, "video");
  1757. if (NULL == dev->vdev) {
  1758. em28xx_errdev("cannot allocate video_device.\n");
  1759. goto fail_unreg;
  1760. }
  1761. if (dev->tuner_type != TUNER_ABSENT)
  1762. dev->vdev->type |= VID_TYPE_TUNER;
  1763. /* register v4l2 video video_device */
  1764. retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1765. video_nr[dev->devno]);
  1766. if (retval) {
  1767. em28xx_errdev("unable to register video device (error=%i).\n",
  1768. retval);
  1769. goto fail_unreg;
  1770. }
  1771. /* Allocate and fill vbi video_device struct */
  1772. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1773. VFL_TYPE_VBI, "vbi");
  1774. /* register v4l2 vbi video_device */
  1775. if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1776. vbi_nr[dev->devno]) < 0) {
  1777. em28xx_errdev("unable to register vbi device\n");
  1778. retval = -ENODEV;
  1779. goto fail_unreg;
  1780. }
  1781. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1782. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1783. VFL_TYPE_RADIO, "radio");
  1784. if (NULL == dev->radio_dev) {
  1785. em28xx_errdev("cannot allocate video_device.\n");
  1786. goto fail_unreg;
  1787. }
  1788. retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1789. radio_nr[dev->devno]);
  1790. if (retval < 0) {
  1791. em28xx_errdev("can't register radio device\n");
  1792. goto fail_unreg;
  1793. }
  1794. em28xx_info("Registered radio device as /dev/radio%d\n",
  1795. dev->radio_dev->minor & 0x1f);
  1796. }
  1797. /* init video dma queues */
  1798. INIT_LIST_HEAD(&dev->vidq.active);
  1799. INIT_LIST_HEAD(&dev->vidq.queued);
  1800. dev->vidq.timeout.function = em28xx_vid_timeout;
  1801. dev->vidq.timeout.data = (unsigned long)dev;
  1802. init_timer(&dev->vidq.timeout);
  1803. if (dev->has_msp34xx) {
  1804. /* Send a reset to other chips via gpio */
  1805. em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1806. msleep(3);
  1807. em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1808. msleep(3);
  1809. }
  1810. video_mux(dev, 0);
  1811. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1812. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1813. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1814. mutex_lock(&em28xx_extension_devlist_lock);
  1815. if (!list_empty(&em28xx_extension_devlist)) {
  1816. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1817. if (ops->id)
  1818. ops->init(dev);
  1819. }
  1820. }
  1821. mutex_unlock(&em28xx_extension_devlist_lock);
  1822. return 0;
  1823. fail_unreg:
  1824. em28xx_release_resources(dev);
  1825. mutex_unlock(&dev->lock);
  1826. kfree(dev);
  1827. return retval;
  1828. }
  1829. #if defined(CONFIG_MODULES) && defined(MODULE)
  1830. static void request_module_async(struct work_struct *work)
  1831. {
  1832. struct em28xx *dev = container_of(work,
  1833. struct em28xx, request_module_wk);
  1834. if (dev->has_audio_class)
  1835. request_module("snd-usb-audio");
  1836. else
  1837. request_module("em28xx-alsa");
  1838. }
  1839. static void request_modules(struct em28xx *dev)
  1840. {
  1841. INIT_WORK(&dev->request_module_wk, request_module_async);
  1842. schedule_work(&dev->request_module_wk);
  1843. }
  1844. #else
  1845. #define request_modules(dev)
  1846. #endif /* CONFIG_MODULES */
  1847. /*
  1848. * em28xx_usb_probe()
  1849. * checks for supported devices
  1850. */
  1851. static int em28xx_usb_probe(struct usb_interface *interface,
  1852. const struct usb_device_id *id)
  1853. {
  1854. const struct usb_endpoint_descriptor *endpoint;
  1855. struct usb_device *udev;
  1856. struct usb_interface *uif;
  1857. struct em28xx *dev = NULL;
  1858. int retval = -ENODEV;
  1859. int i, nr, ifnum;
  1860. udev = usb_get_dev(interface_to_usbdev(interface));
  1861. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1862. /* Check to see next free device and mark as used */
  1863. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1864. em28xx_devused|=1<<nr;
  1865. /* Don't register audio interfaces */
  1866. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1867. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1868. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1869. ifnum,
  1870. interface->altsetting[0].desc.bInterfaceClass);
  1871. em28xx_devused&=~(1<<nr);
  1872. return -ENODEV;
  1873. }
  1874. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1875. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1876. ifnum,
  1877. interface->altsetting[0].desc.bInterfaceClass);
  1878. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1879. /* check if the device has the iso in endpoint at the correct place */
  1880. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1881. USB_ENDPOINT_XFER_ISOC) {
  1882. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1883. em28xx_devused&=~(1<<nr);
  1884. return -ENODEV;
  1885. }
  1886. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1887. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1888. em28xx_devused&=~(1<<nr);
  1889. return -ENODEV;
  1890. }
  1891. if (nr >= EM28XX_MAXBOARDS) {
  1892. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1893. em28xx_devused&=~(1<<nr);
  1894. return -ENOMEM;
  1895. }
  1896. /* allocate memory for our device state and initialize it */
  1897. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1898. if (dev == NULL) {
  1899. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1900. em28xx_devused&=~(1<<nr);
  1901. return -ENOMEM;
  1902. }
  1903. snprintf(dev->name, 29, "em28xx #%d", nr);
  1904. dev->devno = nr;
  1905. dev->model = id->driver_info;
  1906. dev->alt = -1;
  1907. /* Checks if audio is provided by some interface */
  1908. for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
  1909. uif = udev->config->interface[i];
  1910. if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1911. dev->has_audio_class = 1;
  1912. break;
  1913. }
  1914. }
  1915. printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
  1916. dev->has_audio_class ? "Has" : "Doesn't have");
  1917. /* compute alternate max packet sizes */
  1918. uif = udev->actconfig->interface[0];
  1919. dev->num_alt=uif->num_altsetting;
  1920. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1921. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1922. dev->alt_max_pkt_size = kmalloc(32*
  1923. dev->num_alt,GFP_KERNEL);
  1924. if (dev->alt_max_pkt_size == NULL) {
  1925. em28xx_errdev("out of memory!\n");
  1926. em28xx_devused&=~(1<<nr);
  1927. kfree(dev);
  1928. return -ENOMEM;
  1929. }
  1930. for (i = 0; i < dev->num_alt ; i++) {
  1931. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1932. wMaxPacketSize);
  1933. dev->alt_max_pkt_size[i] =
  1934. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1935. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1936. dev->alt_max_pkt_size[i]);
  1937. }
  1938. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1939. dev->model = card[nr];
  1940. /* allocate device struct */
  1941. retval = em28xx_init_dev(&dev, udev, nr);
  1942. if (retval)
  1943. return retval;
  1944. em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
  1945. /* save our data pointer in this interface device */
  1946. usb_set_intfdata(interface, dev);
  1947. request_modules(dev);
  1948. return 0;
  1949. }
  1950. /*
  1951. * em28xx_usb_disconnect()
  1952. * called when the device gets diconencted
  1953. * video device will be unregistered on v4l2_close in case it is still open
  1954. */
  1955. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1956. {
  1957. struct em28xx *dev;
  1958. struct em28xx_ops *ops = NULL;
  1959. dev = usb_get_intfdata(interface);
  1960. usb_set_intfdata(interface, NULL);
  1961. if (!dev)
  1962. return;
  1963. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1964. /* wait until all current v4l2 io is finished then deallocate resources */
  1965. mutex_lock(&dev->lock);
  1966. wake_up_interruptible_all(&dev->open);
  1967. if (dev->users) {
  1968. em28xx_warn
  1969. ("device /dev/video%d is open! Deregistration and memory "
  1970. "deallocation are deferred on close.\n",
  1971. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1972. dev->state |= DEV_MISCONFIGURED;
  1973. em28xx_uninit_isoc(dev);
  1974. dev->state |= DEV_DISCONNECTED;
  1975. wake_up_interruptible(&dev->wait_frame);
  1976. wake_up_interruptible(&dev->wait_stream);
  1977. } else {
  1978. dev->state |= DEV_DISCONNECTED;
  1979. em28xx_release_resources(dev);
  1980. }
  1981. mutex_unlock(&dev->lock);
  1982. mutex_lock(&em28xx_extension_devlist_lock);
  1983. if (!list_empty(&em28xx_extension_devlist)) {
  1984. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1985. ops->fini(dev);
  1986. }
  1987. }
  1988. mutex_unlock(&em28xx_extension_devlist_lock);
  1989. if (!dev->users) {
  1990. kfree(dev->alt_max_pkt_size);
  1991. kfree(dev);
  1992. }
  1993. }
  1994. static struct usb_driver em28xx_usb_driver = {
  1995. .name = "em28xx",
  1996. .probe = em28xx_usb_probe,
  1997. .disconnect = em28xx_usb_disconnect,
  1998. .id_table = em28xx_id_table,
  1999. };
  2000. static int __init em28xx_module_init(void)
  2001. {
  2002. int result;
  2003. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  2004. (EM28XX_VERSION_CODE >> 16) & 0xff,
  2005. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2006. #ifdef SNAPSHOT
  2007. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  2008. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  2009. #endif
  2010. /* register this driver with the USB subsystem */
  2011. result = usb_register(&em28xx_usb_driver);
  2012. if (result)
  2013. em28xx_err(DRIVER_NAME
  2014. " usb_register failed. Error number %d.\n", result);
  2015. return result;
  2016. }
  2017. static void __exit em28xx_module_exit(void)
  2018. {
  2019. /* deregister this driver with the USB subsystem */
  2020. usb_deregister(&em28xx_usb_driver);
  2021. }
  2022. module_init(em28xx_module_init);
  2023. module_exit(em28xx_module_exit);