em28xx-video.c 46 KB

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