em28xx-video.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. /*
  2. em2820-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
  3. Copyright (C) 2005 Markus Rechberger <mrechberger@gmail.com>
  4. Ludovico Cavedon <cavedon@sssup.it>
  5. Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  6. Based on the em2800 driver from Sascha Sommer <saschasommer@freenet.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/usb.h>
  24. #include <linux/i2c.h>
  25. #include <linux/video_decoder.h>
  26. #include "em2820.h"
  27. #include <media/tuner.h>
  28. #define DRIVER_AUTHOR "Markus Rechberger <mrechberger@gmail.com>, " \
  29. "Ludovico Cavedon <cavedon@sssup.it>, " \
  30. "Mauro Carvalho Chehab <mchehab@brturbo.com.br>"
  31. #define DRIVER_NAME "em2820"
  32. #define DRIVER_DESC "Empia em2820 based USB video device driver"
  33. #define EM2820_VERSION_CODE KERNEL_VERSION(0, 0, 1)
  34. #define em2820_videodbg(fmt, arg...) do {\
  35. if (video_debug) \
  36. printk(KERN_INFO "%s %s :"fmt, \
  37. dev->name, __FUNCTION__ , ##arg); } while (0)
  38. MODULE_AUTHOR(DRIVER_AUTHOR);
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. static LIST_HEAD(em2820_devlist);
  42. static unsigned int card[] = {[0 ... (EM2820_MAXBOARDS - 1)] = UNSET };
  43. module_param_array(card, int, NULL, 0444);
  44. MODULE_PARM_DESC(card,"card type");
  45. static int tuner = -1;
  46. module_param(tuner, int, 0444);
  47. MODULE_PARM_DESC(tuner, "tuner type");
  48. static unsigned int video_debug = 0;
  49. module_param(video_debug,int,0644);
  50. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  51. /* supported tv norms */
  52. static struct em2820_tvnorm tvnorms[] = {
  53. {
  54. .name = "PAL",
  55. .id = V4L2_STD_PAL,
  56. .mode = VIDEO_MODE_PAL,
  57. }, {
  58. .name = "NTSC",
  59. .id = V4L2_STD_NTSC,
  60. .mode = VIDEO_MODE_NTSC,
  61. }, {
  62. .name = "SECAM",
  63. .id = V4L2_STD_SECAM,
  64. .mode = VIDEO_MODE_SECAM,
  65. }, {
  66. .name = "PAL-M",
  67. .id = V4L2_STD_PAL_M,
  68. .mode = VIDEO_MODE_PAL,
  69. }
  70. };
  71. #define TVNORMS ARRAY_SIZE(tvnorms)
  72. /* supported controls */
  73. static struct v4l2_queryctrl em2820_qctrl[] = {
  74. {
  75. .id = V4L2_CID_BRIGHTNESS,
  76. .type = V4L2_CTRL_TYPE_INTEGER,
  77. .name = "Brightness",
  78. .minimum = -128,
  79. .maximum = 127,
  80. .step = 1,
  81. .default_value = 0,
  82. .flags = 0,
  83. },{
  84. .id = V4L2_CID_CONTRAST,
  85. .type = V4L2_CTRL_TYPE_INTEGER,
  86. .name = "Contrast",
  87. .minimum = 0x0,
  88. .maximum = 0x1f,
  89. .step = 0x1,
  90. .default_value = 0x10,
  91. .flags = 0,
  92. },{
  93. .id = V4L2_CID_SATURATION,
  94. .type = V4L2_CTRL_TYPE_INTEGER,
  95. .name = "Saturation",
  96. .minimum = 0x0,
  97. .maximum = 0x1f,
  98. .step = 0x1,
  99. .default_value = 0x10,
  100. .flags = 0,
  101. },{
  102. .id = V4L2_CID_AUDIO_VOLUME,
  103. .type = V4L2_CTRL_TYPE_INTEGER,
  104. .name = "Volume",
  105. .minimum = 0x0,
  106. .maximum = 0x1f,
  107. .step = 0x1,
  108. .default_value = 0x1f,
  109. .flags = 0,
  110. },{
  111. .id = V4L2_CID_AUDIO_MUTE,
  112. .type = V4L2_CTRL_TYPE_BOOLEAN,
  113. .name = "Mute",
  114. .minimum = 0,
  115. .maximum = 1,
  116. .step = 1,
  117. .default_value = 1,
  118. .flags = 0,
  119. },{
  120. .id = V4L2_CID_RED_BALANCE,
  121. .type = V4L2_CTRL_TYPE_INTEGER,
  122. .name = "Red chroma balance",
  123. .minimum = -128,
  124. .maximum = 127,
  125. .step = 1,
  126. .default_value = 0,
  127. .flags = 0,
  128. },{
  129. .id = V4L2_CID_BLUE_BALANCE,
  130. .type = V4L2_CTRL_TYPE_INTEGER,
  131. .name = "Blue chroma balance",
  132. .minimum = -128,
  133. .maximum = 127,
  134. .step = 1,
  135. .default_value = 0,
  136. .flags = 0,
  137. },{
  138. .id = V4L2_CID_GAMMA,
  139. .type = V4L2_CTRL_TYPE_INTEGER,
  140. .name = "Gamma",
  141. .minimum = 0x0,
  142. .maximum = 0x3f,
  143. .step = 0x1,
  144. .default_value = 0x20,
  145. .flags = 0,
  146. }
  147. };
  148. static struct usb_driver em2820_usb_driver;
  149. static DECLARE_MUTEX(em2820_sysfs_lock);
  150. static DECLARE_RWSEM(em2820_disconnect);
  151. /********************* v4l2 interface ******************************************/
  152. static inline unsigned long kvirt_to_pa(unsigned long adr)
  153. {
  154. unsigned long kva, ret;
  155. kva = (unsigned long)page_address(vmalloc_to_page((void *)adr));
  156. kva |= adr & (PAGE_SIZE - 1);
  157. ret = __pa(kva);
  158. return ret;
  159. }
  160. /*
  161. * em2820_config()
  162. * inits registers with sane defaults
  163. */
  164. static int em2820_config(struct em2820 *dev)
  165. {
  166. /* Sets I2C speed to 100 KHz */
  167. em2820_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
  168. /* enable vbi capturing */
  169. em2820_audio_usb_mute(dev, 1);
  170. dev->mute = 1; /* maybe not the right place... */
  171. dev->volume = 0x1f;
  172. em2820_audio_analog_set(dev);
  173. em2820_audio_analog_setup(dev);
  174. em2820_outfmt_set_yuv422(dev);
  175. em2820_colorlevels_set_default(dev);
  176. em2820_compression_disable(dev);
  177. return 0;
  178. }
  179. /*
  180. * em2820_config_i2c()
  181. * configure i2c attached devices
  182. */
  183. void em2820_config_i2c(struct em2820 *dev)
  184. {
  185. struct v4l2_frequency f;
  186. struct video_decoder_init em2820_vdi = {.data = NULL };
  187. /* configure decoder */
  188. em2820_i2c_call_clients(dev, DECODER_INIT, &em2820_vdi);
  189. em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
  190. /* em2820_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
  191. /* em2820_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
  192. /* em2820_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
  193. /* em2820_i2c_call_clients(dev,DECODER_DUMP, NULL); */
  194. /* configure tuner */
  195. f.tuner = 0;
  196. f.type = V4L2_TUNER_ANALOG_TV;
  197. f.frequency = 9076; /* FIXME:remove magic number */
  198. dev->ctl_freq = f.frequency;
  199. em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
  200. /* configure tda9887 */
  201. /* em2820_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
  202. }
  203. /*
  204. * em2820_empty_framequeues()
  205. * prepare queues for incoming and outgoing frames
  206. */
  207. static void em2820_empty_framequeues(struct em2820 *dev)
  208. {
  209. u32 i;
  210. INIT_LIST_HEAD(&dev->inqueue);
  211. INIT_LIST_HEAD(&dev->outqueue);
  212. for (i = 0; i < EM2820_NUM_FRAMES; i++) {
  213. dev->frame[i].state = F_UNUSED;
  214. dev->frame[i].buf.bytesused = 0;
  215. }
  216. }
  217. /*
  218. * em2820_v4l2_open()
  219. * inits the device and starts isoc transfer
  220. */
  221. static int em2820_v4l2_open(struct inode *inode, struct file *filp)
  222. {
  223. int minor = iminor(inode);
  224. int errCode = 0;
  225. struct em2820 *h,*dev = NULL;
  226. struct list_head *list;
  227. list_for_each(list,&em2820_devlist) {
  228. h = list_entry(list, struct em2820, devlist);
  229. if (h->vdev->minor == minor) {
  230. dev = h;
  231. }
  232. }
  233. filp->private_data=dev;
  234. em2820_videodbg("users=%d", dev->users);
  235. if (!down_read_trylock(&em2820_disconnect))
  236. return -ERESTARTSYS;
  237. if (dev->users) {
  238. em2820_warn("this driver can be opened only once\n");
  239. up_read(&em2820_disconnect);
  240. return -EBUSY;
  241. }
  242. /* if(dev->vbi_dev->minor == minor){
  243. dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
  244. }*/
  245. if (dev->vdev->minor == minor) {
  246. dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  247. }
  248. init_MUTEX(&dev->fileop_lock); /* to 1 == available */
  249. spin_lock_init(&dev->queue_lock);
  250. init_waitqueue_head(&dev->wait_frame);
  251. init_waitqueue_head(&dev->wait_stream);
  252. down(&dev->lock);
  253. em2820_set_alternate(dev);
  254. dev->width = norm_maxw(dev);
  255. dev->height = norm_maxh(dev);
  256. dev->frame_size = dev->width * dev->height * 2;
  257. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  258. dev->bytesperline = dev->width * 2;
  259. dev->hscale = 0;
  260. dev->vscale = 0;
  261. em2820_capture_start(dev, 1);
  262. em2820_resolution_set(dev);
  263. /* start the transfer */
  264. errCode = em2820_init_isoc(dev);
  265. if (errCode)
  266. goto err;
  267. dev->users++;
  268. filp->private_data = dev;
  269. dev->io = IO_NONE;
  270. dev->stream = STREAM_OFF;
  271. dev->num_frames = 0;
  272. /* prepare queues */
  273. em2820_empty_framequeues(dev);
  274. dev->state |= DEV_INITIALIZED;
  275. err:
  276. up(&dev->lock);
  277. up_read(&em2820_disconnect);
  278. return errCode;
  279. }
  280. /*
  281. * em2820_realease_resources()
  282. * unregisters the v4l2,i2c and usb devices
  283. * called when the device gets disconected or at module unload
  284. */
  285. static void em2820_release_resources(struct em2820 *dev)
  286. {
  287. down(&em2820_sysfs_lock);
  288. em2820_info("V4L2 device /dev/video%d deregistered\n",
  289. dev->vdev->minor);
  290. list_del(&dev->devlist);
  291. video_unregister_device(dev->vdev);
  292. /* video_unregister_device(dev->vbi_dev); */
  293. em2820_i2c_unregister(dev);
  294. usb_put_dev(dev->udev);
  295. up(&em2820_sysfs_lock);
  296. }
  297. /*
  298. * em2820_v4l2_close()
  299. * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
  300. */
  301. static int em2820_v4l2_close(struct inode *inode, struct file *file)
  302. {
  303. int errCode;
  304. int minor = iminor(inode);
  305. struct em2820 *h,*dev = NULL;
  306. struct list_head *list;
  307. list_for_each(list,&em2820_devlist) {
  308. h = list_entry(list, struct em2820, devlist);
  309. if (h->vdev->minor == minor) {
  310. dev = h;
  311. }
  312. }
  313. em2820_videodbg("users=%d", dev->users);
  314. down(&dev->lock);
  315. em2820_uninit_isoc(dev);
  316. em2820_release_buffers(dev);
  317. /* the device is already disconnect, free the remaining resources */
  318. if (dev->state & DEV_DISCONNECTED) {
  319. em2820_release_resources(dev);
  320. up(&dev->lock);
  321. kfree(dev);
  322. return 0;
  323. }
  324. /* set alternate 0 */
  325. dev->alt = 0;
  326. em2820_videodbg("setting alternate 0");
  327. errCode = usb_set_interface(dev->udev, 0, 0);
  328. if (errCode < 0) {
  329. em2820_errdev ("cannot change alternate number to 0 (error=%i)\n",
  330. errCode);
  331. }
  332. dev->users--;
  333. wake_up_interruptible_nr(&dev->open, 1);
  334. up(&dev->lock);
  335. return 0;
  336. }
  337. /*
  338. * em2820_v4l2_read()
  339. * will allocate buffers when called for the first time
  340. */
  341. static ssize_t
  342. em2820_v4l2_read(struct file *filp, char __user * buf, size_t count,
  343. loff_t * f_pos)
  344. {
  345. struct em2820_frame_t *f, *i;
  346. unsigned long lock_flags;
  347. int ret = 0;
  348. struct em2820 *dev = filp->private_data;
  349. if (down_interruptible(&dev->fileop_lock))
  350. return -ERESTARTSYS;
  351. if (dev->state & DEV_DISCONNECTED) {
  352. em2820_videodbg("device not present");
  353. up(&dev->fileop_lock);
  354. return -ENODEV;
  355. }
  356. if (dev->state & DEV_MISCONFIGURED) {
  357. em2820_videodbg("device misconfigured; close and open it again");
  358. up(&dev->fileop_lock);
  359. return -EIO;
  360. }
  361. if (dev->io == IO_MMAP) {
  362. em2820_videodbg ("IO method is set to mmap; close and open"
  363. " the device again to choose the read method");
  364. up(&dev->fileop_lock);
  365. return -EINVAL;
  366. }
  367. if (dev->io == IO_NONE) {
  368. if (!em2820_request_buffers(dev, EM2820_NUM_READ_FRAMES)) {
  369. em2820_errdev("read failed, not enough memory\n");
  370. up(&dev->fileop_lock);
  371. return -ENOMEM;
  372. }
  373. dev->io = IO_READ;
  374. dev->stream = STREAM_ON;
  375. em2820_queue_unusedframes(dev);
  376. }
  377. if (!count) {
  378. up(&dev->fileop_lock);
  379. return 0;
  380. }
  381. if (list_empty(&dev->outqueue)) {
  382. if (filp->f_flags & O_NONBLOCK) {
  383. up(&dev->fileop_lock);
  384. return -EAGAIN;
  385. }
  386. ret = wait_event_interruptible
  387. (dev->wait_frame,
  388. (!list_empty(&dev->outqueue)) ||
  389. (dev->state & DEV_DISCONNECTED));
  390. if (ret) {
  391. up(&dev->fileop_lock);
  392. return ret;
  393. }
  394. if (dev->state & DEV_DISCONNECTED) {
  395. up(&dev->fileop_lock);
  396. return -ENODEV;
  397. }
  398. }
  399. f = list_entry(dev->outqueue.prev, struct em2820_frame_t, frame);
  400. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  401. list_for_each_entry(i, &dev->outqueue, frame)
  402. i->state = F_UNUSED;
  403. INIT_LIST_HEAD(&dev->outqueue);
  404. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  405. em2820_queue_unusedframes(dev);
  406. if (count > f->buf.length)
  407. count = f->buf.length;
  408. if (copy_to_user(buf, f->bufmem, count)) {
  409. up(&dev->fileop_lock);
  410. return -EFAULT;
  411. }
  412. *f_pos += count;
  413. up(&dev->fileop_lock);
  414. return count;
  415. }
  416. /*
  417. * em2820_v4l2_poll()
  418. * will allocate buffers when called for the first time
  419. */
  420. static unsigned int em2820_v4l2_poll(struct file *filp, poll_table * wait)
  421. {
  422. unsigned int mask = 0;
  423. struct em2820 *dev = filp->private_data;
  424. if (down_interruptible(&dev->fileop_lock))
  425. return POLLERR;
  426. if (dev->state & DEV_DISCONNECTED) {
  427. em2820_videodbg("device not present");
  428. } else if (dev->state & DEV_MISCONFIGURED) {
  429. em2820_videodbg("device is misconfigured; close and open it again");
  430. } else {
  431. if (dev->io == IO_NONE) {
  432. if (!em2820_request_buffers
  433. (dev, EM2820_NUM_READ_FRAMES)) {
  434. em2820_warn
  435. ("poll() failed, not enough memory\n");
  436. } else {
  437. dev->io = IO_READ;
  438. dev->stream = STREAM_ON;
  439. }
  440. }
  441. if (dev->io == IO_READ) {
  442. em2820_queue_unusedframes(dev);
  443. poll_wait(filp, &dev->wait_frame, wait);
  444. if (!list_empty(&dev->outqueue))
  445. mask |= POLLIN | POLLRDNORM;
  446. up(&dev->fileop_lock);
  447. return mask;
  448. }
  449. }
  450. up(&dev->fileop_lock);
  451. return POLLERR;
  452. }
  453. /*
  454. * em2820_vm_open()
  455. */
  456. static void em2820_vm_open(struct vm_area_struct *vma)
  457. {
  458. struct em2820_frame_t *f = vma->vm_private_data;
  459. f->vma_use_count++;
  460. }
  461. /*
  462. * em2820_vm_close()
  463. */
  464. static void em2820_vm_close(struct vm_area_struct *vma)
  465. {
  466. /* NOTE: buffers are not freed here */
  467. struct em2820_frame_t *f = vma->vm_private_data;
  468. f->vma_use_count--;
  469. }
  470. static struct vm_operations_struct em2820_vm_ops = {
  471. .open = em2820_vm_open,
  472. .close = em2820_vm_close,
  473. };
  474. /*
  475. * em2820_v4l2_mmap()
  476. */
  477. static int em2820_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  478. {
  479. unsigned long size = vma->vm_end - vma->vm_start,
  480. start = vma->vm_start, pos, page;
  481. u32 i;
  482. struct em2820 *dev = filp->private_data;
  483. if (down_interruptible(&dev->fileop_lock))
  484. return -ERESTARTSYS;
  485. if (dev->state & DEV_DISCONNECTED) {
  486. em2820_videodbg("mmap: device not present");
  487. up(&dev->fileop_lock);
  488. return -ENODEV;
  489. }
  490. if (dev->state & DEV_MISCONFIGURED) {
  491. em2820_videodbg ("mmap: Device is misconfigured; close and "
  492. "open it again");
  493. up(&dev->fileop_lock);
  494. return -EIO;
  495. }
  496. if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
  497. size != PAGE_ALIGN(dev->frame[0].buf.length)) {
  498. up(&dev->fileop_lock);
  499. return -EINVAL;
  500. }
  501. for (i = 0; i < dev->num_frames; i++) {
  502. if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  503. break;
  504. }
  505. if (i == dev->num_frames) {
  506. em2820_videodbg("mmap: user supplied mapping address is out of range");
  507. up(&dev->fileop_lock);
  508. return -EINVAL;
  509. }
  510. /* VM_IO is eventually going to replace PageReserved altogether */
  511. vma->vm_flags |= VM_IO;
  512. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  513. pos = (unsigned long)dev->frame[i].bufmem;
  514. while (size > 0) { /* size is page-aligned */
  515. page = vmalloc_to_pfn((void *)pos);
  516. if (remap_pfn_range(vma, start, page, PAGE_SIZE,
  517. vma->vm_page_prot)) {
  518. em2820_videodbg("mmap: rename page map failed");
  519. up(&dev->fileop_lock);
  520. return -EAGAIN;
  521. }
  522. start += PAGE_SIZE;
  523. pos += PAGE_SIZE;
  524. size -= PAGE_SIZE;
  525. }
  526. vma->vm_ops = &em2820_vm_ops;
  527. vma->vm_private_data = &dev->frame[i];
  528. em2820_vm_open(vma);
  529. up(&dev->fileop_lock);
  530. return 0;
  531. }
  532. /*
  533. * em2820_get_ctrl()
  534. * return the current saturation, brightness or contrast, mute state
  535. */
  536. static int em2820_get_ctrl(struct em2820 *dev, struct v4l2_control *ctrl)
  537. {
  538. s32 tmp;
  539. switch (ctrl->id) {
  540. case V4L2_CID_AUDIO_MUTE:
  541. ctrl->value = dev->mute;
  542. return 0;
  543. case V4L2_CID_AUDIO_VOLUME:
  544. ctrl->value = dev->volume;
  545. return 0;
  546. case V4L2_CID_BRIGHTNESS:
  547. if ((tmp = em2820_brightness_get(dev)) < 0)
  548. return -EIO;
  549. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  550. return 0;
  551. case V4L2_CID_CONTRAST:
  552. if ((ctrl->value = em2820_contrast_get(dev)) < 0)
  553. return -EIO;
  554. return 0;
  555. case V4L2_CID_SATURATION:
  556. if ((ctrl->value = em2820_saturation_get(dev)) < 0)
  557. return -EIO;
  558. return 0;
  559. case V4L2_CID_RED_BALANCE:
  560. if ((tmp = em2820_v_balance_get(dev)) < 0)
  561. return -EIO;
  562. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  563. return 0;
  564. case V4L2_CID_BLUE_BALANCE:
  565. if ((tmp = em2820_u_balance_get(dev)) < 0)
  566. return -EIO;
  567. ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
  568. return 0;
  569. case V4L2_CID_GAMMA:
  570. if ((ctrl->value = em2820_gamma_get(dev)) < 0)
  571. return -EIO;
  572. return 0;
  573. default:
  574. return -EINVAL;
  575. }
  576. }
  577. /*
  578. * em2820_set_ctrl()
  579. * mute or set new saturation, brightness or contrast
  580. */
  581. static int em2820_set_ctrl(struct em2820 *dev, const struct v4l2_control *ctrl)
  582. {
  583. switch (ctrl->id) {
  584. case V4L2_CID_AUDIO_MUTE:
  585. if (ctrl->value != dev->mute) {
  586. dev->mute = ctrl->value;
  587. em2820_audio_usb_mute(dev, ctrl->value);
  588. return em2820_audio_analog_set(dev);
  589. }
  590. return 0;
  591. case V4L2_CID_AUDIO_VOLUME:
  592. dev->volume = ctrl->value;
  593. return em2820_audio_analog_set(dev);
  594. case V4L2_CID_BRIGHTNESS:
  595. return em2820_brightness_set(dev, ctrl->value);
  596. case V4L2_CID_CONTRAST:
  597. return em2820_contrast_set(dev, ctrl->value);
  598. case V4L2_CID_SATURATION:
  599. return em2820_saturation_set(dev, ctrl->value);
  600. case V4L2_CID_RED_BALANCE:
  601. return em2820_v_balance_set(dev, ctrl->value);
  602. case V4L2_CID_BLUE_BALANCE:
  603. return em2820_u_balance_set(dev, ctrl->value);
  604. case V4L2_CID_GAMMA:
  605. return em2820_gamma_set(dev, ctrl->value);
  606. default:
  607. return -EINVAL;
  608. }
  609. }
  610. /*
  611. * em2820_stream_interrupt()
  612. * stops streaming
  613. */
  614. static int em2820_stream_interrupt(struct em2820 *dev)
  615. {
  616. int ret = 0;
  617. /* stop reading from the device */
  618. dev->stream = STREAM_INTERRUPT;
  619. ret = wait_event_timeout(dev->wait_stream,
  620. (dev->stream == STREAM_OFF) ||
  621. (dev->state & DEV_DISCONNECTED),
  622. EM2820_URB_TIMEOUT);
  623. if (dev->state & DEV_DISCONNECTED)
  624. return -ENODEV;
  625. else if (ret) {
  626. dev->state |= DEV_MISCONFIGURED;
  627. em2820_videodbg("device is misconfigured; close and "
  628. "open /dev/video%d again", dev->vdev->minor);
  629. return ret;
  630. }
  631. return 0;
  632. }
  633. static int em2820_set_norm(struct em2820 *dev, int width, int height)
  634. {
  635. unsigned int hscale, vscale;
  636. unsigned int maxh, maxw;
  637. maxw = norm_maxw(dev);
  638. maxh = norm_maxh(dev);
  639. /* width must even because of the YUYV format */
  640. /* height must be even because of interlacing */
  641. height &= 0xfffe;
  642. width &= 0xfffe;
  643. if (height < 32)
  644. height = 32;
  645. if (height > maxh)
  646. height = maxh;
  647. if (width < 48)
  648. width = 48;
  649. if (width > maxw)
  650. width = maxw;
  651. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  652. hscale = 0x3fff;
  653. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  654. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  655. vscale = 0x3fff;
  656. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  657. /* set new image size */
  658. dev->width = width;
  659. dev->height = height;
  660. dev->frame_size = dev->width * dev->height * 2;
  661. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  662. dev->bytesperline = dev->width * 2;
  663. dev->hscale = hscale;
  664. dev->vscale = vscale;
  665. em2820_resolution_set(dev);
  666. return 0;
  667. }
  668. static void video_mux(struct em2820 *dev, int index)
  669. {
  670. int input, ainput;
  671. input = INPUT(index)->vmux;
  672. dev->ctl_input = index;
  673. em2820_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
  674. dev->ctl_ainput = INPUT(index)->amux;
  675. switch (dev->ctl_ainput) {
  676. case 0:
  677. ainput = EM2820_AUDIO_SRC_TUNER;
  678. break;
  679. default:
  680. ainput = EM2820_AUDIO_SRC_LINE;
  681. }
  682. em2820_audio_source(dev, ainput);
  683. }
  684. /*
  685. * em2820_v4l2_do_ioctl()
  686. * This function is _not_ called directly, but from
  687. * em2820_v4l2_ioctl. Userspace
  688. * copying is done already, arg is a kernel pointer.
  689. */
  690. static int em2820_do_ioctl(struct inode *inode, struct file *filp,
  691. struct em2820 *dev, unsigned int cmd, void *arg,
  692. v4l2_kioctl driver_ioctl)
  693. {
  694. int ret;
  695. switch (cmd) {
  696. /* ---------- tv norms ---------- */
  697. case VIDIOC_ENUMSTD:
  698. {
  699. struct v4l2_standard *e = arg;
  700. unsigned int i;
  701. i = e->index;
  702. if (i >= TVNORMS)
  703. return -EINVAL;
  704. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  705. tvnorms[e->index].name);
  706. e->index = i;
  707. if (ret < 0)
  708. return ret;
  709. return 0;
  710. }
  711. case VIDIOC_G_STD:
  712. {
  713. v4l2_std_id *id = arg;
  714. *id = dev->tvnorm->id;
  715. return 0;
  716. }
  717. case VIDIOC_S_STD:
  718. {
  719. v4l2_std_id *id = arg;
  720. unsigned int i;
  721. for (i = 0; i < TVNORMS; i++)
  722. if (*id == tvnorms[i].id)
  723. break;
  724. if (i == TVNORMS)
  725. for (i = 0; i < TVNORMS; i++)
  726. if (*id & tvnorms[i].id)
  727. break;
  728. if (i == TVNORMS)
  729. return -EINVAL;
  730. down(&dev->lock);
  731. dev->tvnorm = &tvnorms[i];
  732. em2820_set_norm(dev, dev->width, dev->height);
  733. /*
  734. dev->width=norm_maxw(dev);
  735. dev->height=norm_maxh(dev);
  736. dev->frame_size=dev->width*dev->height*2;
  737. dev->field_size=dev->frame_size>>1;
  738. dev->bytesperline=dev->width*2;
  739. dev->hscale=0;
  740. dev->vscale=0;
  741. em2820_resolution_set(dev);
  742. */
  743. /*
  744. em2820_uninit_isoc(dev);
  745. em2820_set_alternate(dev);
  746. em2820_capture_start(dev, 1);
  747. em2820_resolution_set(dev);
  748. em2820_init_isoc(dev);
  749. */
  750. em2820_i2c_call_clients(dev, DECODER_SET_NORM,
  751. &tvnorms[i].mode);
  752. em2820_i2c_call_clients(dev, VIDIOC_S_STD,
  753. &dev->tvnorm->id);
  754. up(&dev->lock);
  755. return 0;
  756. }
  757. /* ------ input switching ---------- */
  758. case VIDIOC_ENUMINPUT:
  759. {
  760. struct v4l2_input *i = arg;
  761. unsigned int n;
  762. static const char *iname[] = {
  763. [EM2820_VMUX_COMPOSITE1] = "Composite1",
  764. [EM2820_VMUX_COMPOSITE2] = "Composite2",
  765. [EM2820_VMUX_COMPOSITE3] = "Composite3",
  766. [EM2820_VMUX_COMPOSITE4] = "Composite4",
  767. [EM2820_VMUX_SVIDEO] = "S-Video",
  768. [EM2820_VMUX_TELEVISION] = "Television",
  769. [EM2820_VMUX_CABLE] = "Cable TV",
  770. [EM2820_VMUX_DVB] = "DVB",
  771. [EM2820_VMUX_DEBUG] = "for debug only",
  772. };
  773. n = i->index;
  774. if (n >= MAX_EM2820_INPUT)
  775. return -EINVAL;
  776. if (0 == INPUT(n)->type)
  777. return -EINVAL;
  778. memset(i, 0, sizeof(*i));
  779. i->index = n;
  780. i->type = V4L2_INPUT_TYPE_CAMERA;
  781. strcpy(i->name, iname[INPUT(n)->type]);
  782. if ((EM2820_VMUX_TELEVISION == INPUT(n)->type) ||
  783. (EM2820_VMUX_CABLE == INPUT(n)->type))
  784. i->type = V4L2_INPUT_TYPE_TUNER;
  785. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  786. i->std |= tvnorms[n].id;
  787. return 0;
  788. }
  789. case VIDIOC_G_INPUT:
  790. {
  791. int *i = arg;
  792. *i = dev->ctl_input;
  793. return 0;
  794. }
  795. case VIDIOC_S_INPUT:
  796. {
  797. int *index = arg;
  798. if (*index >= MAX_EM2820_INPUT)
  799. return -EINVAL;
  800. if (0 == INPUT(*index)->type)
  801. return -EINVAL;
  802. down(&dev->lock);
  803. video_mux(dev, *index);
  804. up(&dev->lock);
  805. return 0;
  806. }
  807. case VIDIOC_G_AUDIO:
  808. {
  809. struct v4l2_audio *a = arg;
  810. unsigned int index = a->index;
  811. if (a->index > 1)
  812. return -EINVAL;
  813. memset(a, 0, sizeof(*a));
  814. index = dev->ctl_ainput;
  815. if (index == 0) {
  816. strcpy(a->name, "Television");
  817. } else {
  818. strcpy(a->name, "Line In");
  819. }
  820. a->capability = V4L2_AUDCAP_STEREO;
  821. a->index = index;
  822. return 0;
  823. }
  824. case VIDIOC_S_AUDIO:
  825. {
  826. struct v4l2_audio *a = arg;
  827. if (a->index != dev->ctl_ainput)
  828. return -EINVAL;
  829. return 0;
  830. }
  831. /* --- controls ---------------------------------------------- */
  832. case VIDIOC_QUERYCTRL:
  833. {
  834. struct v4l2_queryctrl *qc = arg;
  835. u8 i, n;
  836. n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
  837. for (i = 0; i < n; i++)
  838. if (qc->id && qc->id == em2820_qctrl[i].id) {
  839. memcpy(qc, &(em2820_qctrl[i]),
  840. sizeof(*qc));
  841. return 0;
  842. }
  843. return -EINVAL;
  844. }
  845. case VIDIOC_G_CTRL:
  846. {
  847. struct v4l2_control *ctrl = arg;
  848. return em2820_get_ctrl(dev, ctrl);
  849. }
  850. case VIDIOC_S_CTRL_OLD: /* ??? */
  851. case VIDIOC_S_CTRL:
  852. {
  853. struct v4l2_control *ctrl = arg;
  854. u8 i, n;
  855. n = sizeof(em2820_qctrl) / sizeof(em2820_qctrl[0]);
  856. for (i = 0; i < n; i++)
  857. if (ctrl->id == em2820_qctrl[i].id) {
  858. if (ctrl->value <
  859. em2820_qctrl[i].minimum
  860. || ctrl->value >
  861. em2820_qctrl[i].maximum)
  862. return -ERANGE;
  863. return em2820_set_ctrl(dev, ctrl);
  864. }
  865. return -EINVAL;
  866. }
  867. /* --- tuner ioctls ------------------------------------------ */
  868. case VIDIOC_G_TUNER:
  869. {
  870. struct v4l2_tuner *t = arg;
  871. int status = 0;
  872. if (0 != t->index)
  873. return -EINVAL;
  874. memset(t, 0, sizeof(*t));
  875. strcpy(t->name, "Tuner");
  876. t->type = V4L2_TUNER_ANALOG_TV;
  877. t->capability = V4L2_TUNER_CAP_NORM;
  878. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  879. /* t->signal = 0xffff;*/
  880. /* em2820_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
  881. /* No way to get signal strength? */
  882. down(&dev->lock);
  883. em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
  884. &status);
  885. up(&dev->lock);
  886. t->signal =
  887. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  888. em2820_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal,
  889. t->afc);
  890. return 0;
  891. }
  892. case VIDIOC_S_TUNER:
  893. {
  894. struct v4l2_tuner *t = arg;
  895. int status = 0;
  896. if (0 != t->index)
  897. return -EINVAL;
  898. memset(t, 0, sizeof(*t));
  899. strcpy(t->name, "Tuner");
  900. t->type = V4L2_TUNER_ANALOG_TV;
  901. t->capability = V4L2_TUNER_CAP_NORM;
  902. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  903. /* t->signal = 0xffff; */
  904. /* No way to get signal strength? */
  905. down(&dev->lock);
  906. em2820_i2c_call_clients(dev, DECODER_GET_STATUS,
  907. &status);
  908. up(&dev->lock);
  909. t->signal =
  910. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  911. em2820_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
  912. t->signal, t->afc);
  913. return 0;
  914. }
  915. case VIDIOC_G_FREQUENCY:
  916. {
  917. struct v4l2_frequency *f = arg;
  918. memset(f, 0, sizeof(*f));
  919. f->type = V4L2_TUNER_ANALOG_TV;
  920. f->frequency = dev->ctl_freq;
  921. return 0;
  922. }
  923. case VIDIOC_S_FREQUENCY:
  924. {
  925. struct v4l2_frequency *f = arg;
  926. if (0 != f->tuner)
  927. return -EINVAL;
  928. if (V4L2_TUNER_ANALOG_TV != f->type)
  929. return -EINVAL;
  930. down(&dev->lock);
  931. dev->ctl_freq = f->frequency;
  932. em2820_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  933. up(&dev->lock);
  934. return 0;
  935. }
  936. case VIDIOC_CROPCAP:
  937. {
  938. struct v4l2_cropcap *cc = arg;
  939. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  940. return -EINVAL;
  941. cc->bounds.left = 0;
  942. cc->bounds.top = 0;
  943. cc->bounds.width = dev->width;
  944. cc->bounds.height = dev->height;
  945. cc->defrect = cc->bounds;
  946. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  947. cc->pixelaspect.denominator = 59;
  948. return 0;
  949. }
  950. case VIDIOC_STREAMON:
  951. {
  952. int *type = arg;
  953. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  954. || dev->io != IO_MMAP)
  955. return -EINVAL;
  956. if (list_empty(&dev->inqueue))
  957. return -EINVAL;
  958. dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
  959. em2820_videodbg("VIDIOC_STREAMON: starting stream");
  960. return 0;
  961. }
  962. case VIDIOC_STREAMOFF:
  963. {
  964. int *type = arg;
  965. int ret;
  966. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  967. || dev->io != IO_MMAP)
  968. return -EINVAL;
  969. if (dev->stream == STREAM_ON) {
  970. em2820_videodbg ("VIDIOC_STREAMOFF: interrupting stream");
  971. if ((ret = em2820_stream_interrupt(dev)))
  972. return ret;
  973. }
  974. em2820_empty_framequeues(dev);
  975. return 0;
  976. }
  977. default:
  978. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  979. driver_ioctl);
  980. }
  981. return 0;
  982. }
  983. /*
  984. * em2820_v4l2_do_ioctl()
  985. * This function is _not_ called directly, but from
  986. * em2820_v4l2_ioctl. Userspace
  987. * copying is done already, arg is a kernel pointer.
  988. */
  989. static int em2820_video_do_ioctl(struct inode *inode, struct file *filp,
  990. unsigned int cmd, void *arg)
  991. {
  992. struct em2820 *dev = filp->private_data;
  993. if (!dev)
  994. return -ENODEV;
  995. if (video_debug > 1)
  996. em2820_print_ioctl(dev->name,cmd);
  997. switch (cmd) {
  998. /* --- capabilities ------------------------------------------ */
  999. case VIDIOC_QUERYCAP:
  1000. {
  1001. struct v4l2_capability *cap = arg;
  1002. memset(cap, 0, sizeof(*cap));
  1003. strlcpy(cap->driver, "em2820", sizeof(cap->driver));
  1004. strlcpy(cap->card, em2820_boards[dev->model].name,
  1005. sizeof(cap->card));
  1006. strlcpy(cap->bus_info, dev->udev->dev.bus_id,
  1007. sizeof(cap->bus_info));
  1008. cap->version = EM2820_VERSION_CODE;
  1009. cap->capabilities =
  1010. V4L2_CAP_VIDEO_CAPTURE |
  1011. V4L2_CAP_AUDIO |
  1012. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1013. if (dev->has_tuner)
  1014. cap->capabilities |= V4L2_CAP_TUNER;
  1015. return 0;
  1016. }
  1017. /* --- capture ioctls ---------------------------------------- */
  1018. case VIDIOC_ENUM_FMT:
  1019. {
  1020. struct v4l2_fmtdesc *fmtd = arg;
  1021. if (fmtd->index != 0)
  1022. return -EINVAL;
  1023. memset(fmtd, 0, sizeof(*fmtd));
  1024. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1025. strcpy(fmtd->description, "Packed YUY2");
  1026. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1027. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1028. return 0;
  1029. }
  1030. case VIDIOC_G_FMT:
  1031. {
  1032. struct v4l2_format *format = arg;
  1033. em2820_videodbg("VIDIOC_G_FMT: type=%s",
  1034. format->type ==
  1035. V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  1036. "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
  1037. V4L2_BUF_TYPE_VBI_CAPTURE ?
  1038. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  1039. "not supported");
  1040. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1041. return -EINVAL;
  1042. format->fmt.pix.width = dev->width;
  1043. format->fmt.pix.height = dev->height;
  1044. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1045. format->fmt.pix.bytesperline = dev->bytesperline;
  1046. format->fmt.pix.sizeimage = dev->frame_size;
  1047. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1048. format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  1049. em2820_videodbg("VIDIOC_G_FMT: %dx%d", dev->width,
  1050. dev->height);
  1051. return 0;
  1052. }
  1053. case VIDIOC_TRY_FMT:
  1054. case VIDIOC_S_FMT:
  1055. {
  1056. struct v4l2_format *format = arg;
  1057. u32 i;
  1058. int ret = 0;
  1059. int width = format->fmt.pix.width;
  1060. int height = format->fmt.pix.height;
  1061. unsigned int hscale, vscale;
  1062. unsigned int maxh, maxw;
  1063. maxw = norm_maxw(dev);
  1064. maxh = norm_maxh(dev);
  1065. /* int both_fields; */
  1066. em2820_videodbg("%s: type=%s",
  1067. cmd ==
  1068. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1069. "VIDIOC_S_FMT",
  1070. format->type ==
  1071. V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  1072. "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
  1073. V4L2_BUF_TYPE_VBI_CAPTURE ?
  1074. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  1075. "not supported");
  1076. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1077. return -EINVAL;
  1078. em2820_videodbg("%s: requested %dx%d",
  1079. cmd ==
  1080. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1081. "VIDIOC_S_FMT", format->fmt.pix.width,
  1082. format->fmt.pix.height);
  1083. /* FIXME: Move some code away from here */
  1084. /* width must even because of the YUYV format */
  1085. /* height must be even because of interlacing */
  1086. height &= 0xfffe;
  1087. width &= 0xfffe;
  1088. if (height < 32)
  1089. height = 32;
  1090. if (height > maxh)
  1091. height = maxh;
  1092. if (width < 48)
  1093. width = 48;
  1094. if (width > maxw)
  1095. width = maxw;
  1096. /* FIXME*/
  1097. if(dev->is_em2800){
  1098. /* we only know how to scale to 50% */
  1099. if(height % (maxh / 2))
  1100. height=maxh;
  1101. if(width % (maxw / 2))
  1102. width=maxw;
  1103. /* larger resoltion don't seem to work either */
  1104. if(width == maxw && height == maxh)
  1105. width /= 2;
  1106. }
  1107. if ((hscale =
  1108. (((unsigned long)maxw) << 12) / width - 4096L) >=
  1109. 0x4000)
  1110. hscale = 0x3fff;
  1111. width =
  1112. (((unsigned long)maxw) << 12) / (hscale + 4096L);
  1113. if ((vscale =
  1114. (((unsigned long)maxh) << 12) / height - 4096L) >=
  1115. 0x4000)
  1116. vscale = 0x3fff;
  1117. height =
  1118. (((unsigned long)maxh) << 12) / (vscale + 4096L);
  1119. format->fmt.pix.width = width;
  1120. format->fmt.pix.height = height;
  1121. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  1122. format->fmt.pix.bytesperline = width * 2;
  1123. format->fmt.pix.sizeimage = width * 2 * height;
  1124. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1125. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1126. em2820_videodbg("%s: returned %dx%d (%d, %d)",
  1127. cmd ==
  1128. VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
  1129. "VIDIOC_S_FMT", format->fmt.pix.width,
  1130. format->fmt.pix.height, hscale, vscale);
  1131. if (cmd == VIDIOC_TRY_FMT)
  1132. return 0;
  1133. for (i = 0; i < dev->num_frames; i++)
  1134. if (dev->frame[i].vma_use_count) {
  1135. em2820_videodbg("VIDIOC_S_FMT failed. "
  1136. "Unmap the buffers first.");
  1137. return -EINVAL;
  1138. }
  1139. /* stop io in case it is already in progress */
  1140. if (dev->stream == STREAM_ON) {
  1141. em2820_videodbg("VIDIOC_SET_FMT: interupting stream");
  1142. if ((ret = em2820_stream_interrupt(dev)))
  1143. return ret;
  1144. }
  1145. em2820_release_buffers(dev);
  1146. dev->io = IO_NONE;
  1147. /* set new image size */
  1148. dev->width = width;
  1149. dev->height = height;
  1150. dev->frame_size = dev->width * dev->height * 2;
  1151. dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
  1152. dev->bytesperline = dev->width * 2;
  1153. dev->hscale = hscale;
  1154. dev->vscale = vscale;
  1155. /* dev->both_fileds = both_fileds; */
  1156. em2820_uninit_isoc(dev);
  1157. em2820_set_alternate(dev);
  1158. em2820_capture_start(dev, 1);
  1159. em2820_resolution_set(dev);
  1160. em2820_init_isoc(dev);
  1161. return 0;
  1162. }
  1163. /* --- streaming capture ------------------------------------- */
  1164. case VIDIOC_REQBUFS:
  1165. {
  1166. struct v4l2_requestbuffers *rb = arg;
  1167. u32 i;
  1168. int ret;
  1169. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1170. rb->memory != V4L2_MEMORY_MMAP)
  1171. return -EINVAL;
  1172. if (dev->io == IO_READ) {
  1173. em2820_videodbg ("method is set to read;"
  1174. " close and open the device again to"
  1175. " choose the mmap I/O method");
  1176. return -EINVAL;
  1177. }
  1178. for (i = 0; i < dev->num_frames; i++)
  1179. if (dev->frame[i].vma_use_count) {
  1180. em2820_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped");
  1181. return -EINVAL;
  1182. }
  1183. if (dev->stream == STREAM_ON) {
  1184. em2820_videodbg("VIDIOC_REQBUFS: interrupting stream");
  1185. if ((ret = em2820_stream_interrupt(dev)))
  1186. return ret;
  1187. }
  1188. em2820_empty_framequeues(dev);
  1189. em2820_release_buffers(dev);
  1190. if (rb->count)
  1191. rb->count =
  1192. em2820_request_buffers(dev, rb->count);
  1193. dev->frame_current = NULL;
  1194. em2820_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i",
  1195. rb->count);
  1196. dev->io = rb->count ? IO_MMAP : IO_NONE;
  1197. return 0;
  1198. }
  1199. case VIDIOC_QUERYBUF:
  1200. {
  1201. struct v4l2_buffer *b = arg;
  1202. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1203. b->index >= dev->num_frames || dev->io != IO_MMAP)
  1204. return -EINVAL;
  1205. memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
  1206. if (dev->frame[b->index].vma_use_count) {
  1207. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1208. }
  1209. if (dev->frame[b->index].state == F_DONE)
  1210. b->flags |= V4L2_BUF_FLAG_DONE;
  1211. else if (dev->frame[b->index].state != F_UNUSED)
  1212. b->flags |= V4L2_BUF_FLAG_QUEUED;
  1213. return 0;
  1214. }
  1215. case VIDIOC_QBUF:
  1216. {
  1217. struct v4l2_buffer *b = arg;
  1218. unsigned long lock_flags;
  1219. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1220. b->index >= dev->num_frames || dev->io != IO_MMAP) {
  1221. return -EINVAL;
  1222. }
  1223. if (dev->frame[b->index].state != F_UNUSED) {
  1224. return -EAGAIN;
  1225. }
  1226. dev->frame[b->index].state = F_QUEUED;
  1227. /* add frame to fifo */
  1228. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1229. list_add_tail(&dev->frame[b->index].frame,
  1230. &dev->inqueue);
  1231. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1232. return 0;
  1233. }
  1234. case VIDIOC_DQBUF:
  1235. {
  1236. struct v4l2_buffer *b = arg;
  1237. struct em2820_frame_t *f;
  1238. unsigned long lock_flags;
  1239. int ret = 0;
  1240. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1241. || dev->io != IO_MMAP)
  1242. return -EINVAL;
  1243. if (list_empty(&dev->outqueue)) {
  1244. if (dev->stream == STREAM_OFF)
  1245. return -EINVAL;
  1246. if (filp->f_flags & O_NONBLOCK)
  1247. return -EAGAIN;
  1248. ret = wait_event_interruptible
  1249. (dev->wait_frame,
  1250. (!list_empty(&dev->outqueue)) ||
  1251. (dev->state & DEV_DISCONNECTED));
  1252. if (ret)
  1253. return ret;
  1254. if (dev->state & DEV_DISCONNECTED)
  1255. return -ENODEV;
  1256. }
  1257. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1258. f = list_entry(dev->outqueue.next,
  1259. struct em2820_frame_t, frame);
  1260. list_del(dev->outqueue.next);
  1261. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1262. f->state = F_UNUSED;
  1263. memcpy(b, &f->buf, sizeof(*b));
  1264. if (f->vma_use_count)
  1265. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1266. return 0;
  1267. }
  1268. default:
  1269. return em2820_do_ioctl(inode, filp, dev, cmd, arg,
  1270. em2820_video_do_ioctl);
  1271. }
  1272. return 0;
  1273. }
  1274. /*
  1275. * em2820_v4l2_ioctl()
  1276. * handle v4l2 ioctl the main action happens in em2820_v4l2_do_ioctl()
  1277. */
  1278. static int em2820_v4l2_ioctl(struct inode *inode, struct file *filp,
  1279. unsigned int cmd, unsigned long arg)
  1280. {
  1281. int ret = 0;
  1282. struct em2820 *dev = filp->private_data;
  1283. if (down_interruptible(&dev->fileop_lock))
  1284. return -ERESTARTSYS;
  1285. if (dev->state & DEV_DISCONNECTED) {
  1286. em2820_errdev("v4l2 ioctl: device not present\n");
  1287. up(&dev->fileop_lock);
  1288. return -ENODEV;
  1289. }
  1290. if (dev->state & DEV_MISCONFIGURED) {
  1291. em2820_errdev
  1292. ("v4l2 ioctl: device is misconfigured; close and open it again\n");
  1293. up(&dev->fileop_lock);
  1294. return -EIO;
  1295. }
  1296. ret = video_usercopy(inode, filp, cmd, arg, em2820_video_do_ioctl);
  1297. up(&dev->fileop_lock);
  1298. return ret;
  1299. }
  1300. static struct file_operations em2820_v4l_fops = {
  1301. .owner = THIS_MODULE,
  1302. .open = em2820_v4l2_open,
  1303. .release = em2820_v4l2_close,
  1304. .ioctl = em2820_v4l2_ioctl,
  1305. .read = em2820_v4l2_read,
  1306. .poll = em2820_v4l2_poll,
  1307. .mmap = em2820_v4l2_mmap,
  1308. .llseek = no_llseek,
  1309. };
  1310. /******************************** usb interface *****************************************/
  1311. /*
  1312. * em2820_init_dev()
  1313. * allocates and inits the device structs, registers i2c bus and v4l device
  1314. */
  1315. static int em2820_init_dev(struct em2820 **devhandle, struct usb_device *udev,
  1316. int minor, int model)
  1317. {
  1318. struct em2820 *dev = *devhandle;
  1319. int retval = -ENOMEM;
  1320. int errCode, i;
  1321. unsigned int maxh, maxw;
  1322. struct usb_interface *uif;
  1323. dev->udev = udev;
  1324. dev->model = model;
  1325. init_MUTEX(&dev->lock);
  1326. init_waitqueue_head(&dev->open);
  1327. dev->em2820_write_regs = em2820_write_regs;
  1328. dev->em2820_read_reg = em2820_read_reg;
  1329. dev->em2820_read_reg_req_len = em2820_read_reg_req_len;
  1330. dev->em2820_write_regs_req = em2820_write_regs_req;
  1331. dev->em2820_read_reg_req = em2820_read_reg_req;
  1332. dev->is_em2800 = em2820_boards[model].is_em2800;
  1333. dev->has_tuner = em2820_boards[model].has_tuner;
  1334. dev->has_msp34xx = em2820_boards[model].has_msp34xx;
  1335. dev->tda9887_conf = em2820_boards[model].tda9887_conf;
  1336. dev->decoder = em2820_boards[model].decoder;
  1337. if (tuner >= 0)
  1338. dev->tuner_type = tuner;
  1339. else
  1340. dev->tuner_type = em2820_boards[model].tuner_type;
  1341. dev->video_inputs = em2820_boards[model].vchannels;
  1342. for (i = 0; i < TVNORMS; i++)
  1343. if (em2820_boards[model].norm == tvnorms[i].mode)
  1344. break;
  1345. if (i == TVNORMS)
  1346. i = 0;
  1347. dev->tvnorm = &tvnorms[i]; /* set default norm */
  1348. em2820_videodbg("tvnorm=%s\n", dev->tvnorm->name);
  1349. maxw = norm_maxw(dev);
  1350. maxh = norm_maxh(dev);
  1351. /* set default image size */
  1352. dev->width = maxw;
  1353. dev->height = maxh;
  1354. dev->interlaced = EM2820_INTERLACED_DEFAULT;
  1355. dev->field_size = dev->width * dev->height;
  1356. dev->frame_size =
  1357. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1358. dev->bytesperline = dev->width * 2;
  1359. dev->hscale = 0;
  1360. dev->vscale = 0;
  1361. dev->ctl_input = 2;
  1362. /* setup video picture settings for saa7113h */
  1363. memset(&dev->vpic, 0, sizeof(dev->vpic));
  1364. dev->vpic.colour = 128 << 8;
  1365. dev->vpic.hue = 128 << 8;
  1366. dev->vpic.brightness = 128 << 8;
  1367. dev->vpic.contrast = 192 << 8;
  1368. dev->vpic.whiteness = 128 << 8; /* This one isn't used */
  1369. dev->vpic.depth = 16;
  1370. dev->vpic.palette = VIDEO_PALETTE_YUV422;
  1371. /* compute alternate max packet sizes */
  1372. uif = dev->udev->actconfig->interface[0];
  1373. dev->alt_max_pkt_size[0] = 0;
  1374. for (i = 1; i <= EM2820_MAX_ALT && i < uif->num_altsetting ; i++) {
  1375. u16 tmp =
  1376. le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1377. wMaxPacketSize);
  1378. dev->alt_max_pkt_size[i] =
  1379. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1380. }
  1381. #ifdef CONFIG_MODULES
  1382. /* request some modules */
  1383. if (dev->decoder == EM2820_SAA7113)
  1384. request_module("saa7113");
  1385. if (dev->decoder == EM2820_SAA7114)
  1386. request_module("saa7114");
  1387. if (dev->decoder == EM2820_TVP5150)
  1388. request_module("tvp5150");
  1389. if (dev->has_tuner)
  1390. request_module("tuner");
  1391. if (dev->tda9887_conf)
  1392. request_module("tda9887");
  1393. #endif
  1394. errCode = em2820_config(dev);
  1395. if (errCode) {
  1396. em2820_errdev("error configuring device\n");
  1397. kfree(dev);
  1398. return -ENOMEM;
  1399. }
  1400. down(&dev->lock);
  1401. /* register i2c bus */
  1402. em2820_i2c_register(dev);
  1403. /* Do board specific init and eeprom reading */
  1404. em2820_card_setup(dev);
  1405. /* configure the device */
  1406. em2820_config_i2c(dev);
  1407. up(&dev->lock);
  1408. errCode = em2820_config(dev);
  1409. #ifdef CONFIG_MODULES
  1410. if (dev->has_msp34xx)
  1411. request_module("msp3400");
  1412. #endif
  1413. /* allocate and fill v4l2 device struct */
  1414. dev->vdev = video_device_alloc();
  1415. if (NULL == dev->vdev) {
  1416. em2820_errdev("cannot allocate video_device.\n");
  1417. kfree(dev);
  1418. return -ENOMEM;
  1419. }
  1420. dev->vdev->owner = THIS_MODULE;
  1421. dev->vdev->type = VID_TYPE_CAPTURE;
  1422. if (dev->has_tuner)
  1423. dev->vdev->type |= VID_TYPE_TUNER;
  1424. dev->vdev->hardware = 0;
  1425. dev->vdev->fops = &em2820_v4l_fops;
  1426. dev->vdev->minor = -1;
  1427. dev->vdev->dev = &dev->udev->dev;
  1428. dev->vdev->release = video_device_release;
  1429. snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
  1430. "em2820 video");
  1431. list_add_tail(&dev->devlist,&em2820_devlist);
  1432. /* register v4l2 device */
  1433. down(&dev->lock);
  1434. if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
  1435. em2820_errdev("unable to register video device (error=%i).\n",
  1436. retval);
  1437. up(&dev->lock);
  1438. list_del(&dev->devlist);
  1439. video_device_release(dev->vdev);
  1440. kfree(dev);
  1441. return -ENODEV;
  1442. }
  1443. if (dev->has_msp34xx) {
  1444. /* Send a reset to other chips via gpio */
  1445. em2820_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1446. udelay(2500);
  1447. em2820_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1448. udelay(2500);
  1449. }
  1450. video_mux(dev, 0);
  1451. up(&dev->lock);
  1452. em2820_info("V4L2 device registered as /dev/video%d\n",
  1453. dev->vdev->minor);
  1454. return 0;
  1455. }
  1456. /*
  1457. * em2820_usb_probe()
  1458. * checks for supported devices
  1459. */
  1460. static int em2820_usb_probe(struct usb_interface *interface,
  1461. const struct usb_device_id *id)
  1462. {
  1463. const struct usb_endpoint_descriptor *endpoint;
  1464. struct usb_device *udev;
  1465. struct em2820 *dev = NULL;
  1466. int retval = -ENODEV;
  1467. int model,i,nr,ifnum;
  1468. udev = usb_get_dev(interface_to_usbdev(interface));
  1469. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1470. em2820_err(DRIVER_NAME " new device (%04x:%04x): interface %i, class %i\n",
  1471. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1472. ifnum,
  1473. interface->altsetting[0].desc.bInterfaceClass);
  1474. /* Don't register audio interfaces */
  1475. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO)
  1476. return -ENODEV;
  1477. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1478. /* check if the the device has the iso in endpoint at the correct place */
  1479. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1480. USB_ENDPOINT_XFER_ISOC) {
  1481. em2820_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1482. return -ENODEV;
  1483. }
  1484. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1485. em2820_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1486. return -ENODEV;
  1487. }
  1488. model=id->driver_info;
  1489. nr=interface->minor;
  1490. if (nr>EM2820_MAXBOARDS) {
  1491. printk ("em2820: Supports only %i em28xx boards.\n",EM2820_MAXBOARDS);
  1492. return -ENOMEM;
  1493. }
  1494. /* allocate memory for our device state and initialize it */
  1495. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  1496. if (dev == NULL) {
  1497. em2820_err(DRIVER_NAME ": out of memory!\n");
  1498. return -ENOMEM;
  1499. }
  1500. memset(dev, 0, sizeof(*dev));
  1501. snprintf(dev->name, 29, "em2820 #%d", nr);
  1502. if ((card[nr]>=0)&&(card[nr]<em2820_bcount))
  1503. model=card[nr];
  1504. if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
  1505. printk( "%s: Your board has no eeprom inside it and thus can't\n"
  1506. "%s: be autodetected. Please pass card=<n> insmod option to\n"
  1507. "%s: workaround that. Redirect complaints to the vendor of\n"
  1508. "%s: the TV card. Best regards,\n"
  1509. "%s: -- tux\n",
  1510. dev->name,dev->name,dev->name,dev->name,dev->name);
  1511. printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
  1512. dev->name);
  1513. for (i = 0; i < em2820_bcount; i++) {
  1514. printk("%s: card=%d -> %s\n",
  1515. dev->name, i, em2820_boards[i].name);
  1516. }
  1517. }
  1518. /* allocate device struct */
  1519. retval = em2820_init_dev(&dev, udev, nr, model);
  1520. if (retval)
  1521. return retval;
  1522. em2820_info("Found %s\n", em2820_boards[model].name);
  1523. /* save our data pointer in this interface device */
  1524. usb_set_intfdata(interface, dev);
  1525. return 0;
  1526. }
  1527. /*
  1528. * em2820_usb_disconnect()
  1529. * called when the device gets diconencted
  1530. * video device will be unregistered on v4l2_close in case it is still open
  1531. */
  1532. static void em2820_usb_disconnect(struct usb_interface *interface)
  1533. {
  1534. struct em2820 *dev = usb_get_intfdata(interface);
  1535. usb_set_intfdata(interface, NULL);
  1536. if (!dev)
  1537. return;
  1538. down_write(&em2820_disconnect);
  1539. down(&dev->lock);
  1540. em2820_info("disconnecting %s\n", dev->vdev->name);
  1541. wake_up_interruptible_all(&dev->open);
  1542. if (dev->users) {
  1543. em2820_warn
  1544. ("device /dev/video%d is open! Deregistration and memory "
  1545. "deallocation are deferred on close.\n", dev->vdev->minor);
  1546. dev->state |= DEV_MISCONFIGURED;
  1547. em2820_uninit_isoc(dev);
  1548. dev->state |= DEV_DISCONNECTED;
  1549. wake_up_interruptible(&dev->wait_frame);
  1550. wake_up_interruptible(&dev->wait_stream);
  1551. } else {
  1552. dev->state |= DEV_DISCONNECTED;
  1553. em2820_release_resources(dev);
  1554. }
  1555. up(&dev->lock);
  1556. if (!dev->users)
  1557. kfree(dev);
  1558. up_write(&em2820_disconnect);
  1559. }
  1560. static struct usb_driver em2820_usb_driver = {
  1561. .owner = THIS_MODULE,
  1562. .name = "em2820",
  1563. .probe = em2820_usb_probe,
  1564. .disconnect = em2820_usb_disconnect,
  1565. .id_table = em2820_id_table,
  1566. };
  1567. static int __init em2820_module_init(void)
  1568. {
  1569. int result;
  1570. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1571. (EM2820_VERSION_CODE >> 16) & 0xff,
  1572. (EM2820_VERSION_CODE >> 8) & 0xff, EM2820_VERSION_CODE & 0xff);
  1573. #ifdef SNAPSHOT
  1574. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1575. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1576. #endif
  1577. /* register this driver with the USB subsystem */
  1578. result = usb_register(&em2820_usb_driver);
  1579. if (result)
  1580. em2820_err(DRIVER_NAME
  1581. " usb_register failed. Error number %d.\n", result);
  1582. return result;
  1583. }
  1584. static void __exit em2820_module_exit(void)
  1585. {
  1586. /* deregister this driver with the USB subsystem */
  1587. usb_deregister(&em2820_usb_driver);
  1588. }
  1589. module_init(em2820_module_init);
  1590. module_exit(em2820_module_exit);