em28xx-video.c 44 KB

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