em28xx-video.c 46 KB

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