usbvision-video.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. /*
  2. * USB USBVISION Video device driver 0.9.9
  3. *
  4. *
  5. *
  6. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  7. *
  8. * This module is part of usbvision driver project.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Let's call the version 0.... until compression decoding is completely
  25. * implemented.
  26. *
  27. * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
  28. * It was based on USB CPiA driver written by Peter Pregler,
  29. * Scott J. Bertin and Johannes Erdfelt
  30. * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
  31. * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
  32. * Updates to driver completed by Dwaine P. Garden
  33. *
  34. *
  35. * TODO:
  36. * - use submit_urb for all setup packets
  37. * - Fix memory settings for nt1004. It is 4 times as big as the
  38. * nt1003 memory.
  39. * - Add audio on endpoint 3 for nt1004 chip.
  40. * Seems impossible, needs a codec interface. Which one?
  41. * - Clean up the driver.
  42. * - optimization for performance.
  43. * - Add Videotext capability (VBI). Working on it.....
  44. * - Check audio for other devices
  45. *
  46. */
  47. #include <linux/version.h>
  48. #include <linux/kernel.h>
  49. #include <linux/list.h>
  50. #include <linux/timer.h>
  51. #include <linux/slab.h>
  52. #include <linux/mm.h>
  53. #include <linux/utsname.h>
  54. #include <linux/highmem.h>
  55. #include <linux/videodev.h>
  56. #include <linux/vmalloc.h>
  57. #include <linux/module.h>
  58. #include <linux/init.h>
  59. #include <linux/spinlock.h>
  60. #include <asm/io.h>
  61. #include <linux/videodev2.h>
  62. #include <linux/video_decoder.h>
  63. #include <linux/i2c.h>
  64. #include <media/saa7115.h>
  65. #include <media/v4l2-common.h>
  66. #include <media/v4l2-ioctl.h>
  67. #include <media/tuner.h>
  68. #include <linux/workqueue.h>
  69. #ifdef CONFIG_KMOD
  70. #include <linux/kmod.h>
  71. #endif
  72. #include "usbvision.h"
  73. #include "usbvision-cards.h"
  74. #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
  75. Dwaine Garden <DwaineGarden@rogers.com>"
  76. #define DRIVER_NAME "usbvision"
  77. #define DRIVER_ALIAS "USBVision"
  78. #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
  79. #define DRIVER_LICENSE "GPL"
  80. #define USBVISION_DRIVER_VERSION_MAJOR 0
  81. #define USBVISION_DRIVER_VERSION_MINOR 9
  82. #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
  83. #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
  84. USBVISION_DRIVER_VERSION_MINOR,\
  85. USBVISION_DRIVER_VERSION_PATCHLEVEL)
  86. #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
  87. "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
  88. "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
  89. #define ENABLE_HEXDUMP 0 /* Enable if you need it */
  90. #ifdef USBVISION_DEBUG
  91. #define PDEBUG(level, fmt, args...) { \
  92. if (video_debug & (level)) \
  93. info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
  94. }
  95. #else
  96. #define PDEBUG(level, fmt, args...) do {} while(0)
  97. #endif
  98. #define DBG_IO 1<<1
  99. #define DBG_PROBE 1<<2
  100. #define DBG_MMAP 1<<3
  101. //String operations
  102. #define rmspace(str) while(*str==' ') str++;
  103. #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
  104. /* sequential number of usbvision device */
  105. static int usbvision_nr;
  106. static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
  107. { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
  108. { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
  109. { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
  110. { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
  111. { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
  112. { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
  113. { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
  114. { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
  115. };
  116. /* Function prototypes */
  117. static void usbvision_release(struct usb_usbvision *usbvision);
  118. /* Default initialization of device driver parameters */
  119. /* Set the default format for ISOC endpoint */
  120. static int isocMode = ISOC_MODE_COMPRESS;
  121. /* Set the default Debug Mode of the device driver */
  122. static int video_debug;
  123. /* Set the default device to power on at startup */
  124. static int PowerOnAtOpen = 1;
  125. /* Sequential Number of Video Device */
  126. static int video_nr = -1;
  127. /* Sequential Number of Radio Device */
  128. static int radio_nr = -1;
  129. /* Sequential Number of VBI Device */
  130. static int vbi_nr = -1;
  131. /* Grab parameters for the device driver */
  132. /* Showing parameters under SYSFS */
  133. module_param(isocMode, int, 0444);
  134. module_param(video_debug, int, 0444);
  135. module_param(PowerOnAtOpen, int, 0444);
  136. module_param(video_nr, int, 0444);
  137. module_param(radio_nr, int, 0444);
  138. module_param(vbi_nr, int, 0444);
  139. MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
  140. MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
  141. MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
  142. MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
  143. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  144. MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
  145. // Misc stuff
  146. MODULE_AUTHOR(DRIVER_AUTHOR);
  147. MODULE_DESCRIPTION(DRIVER_DESC);
  148. MODULE_LICENSE(DRIVER_LICENSE);
  149. MODULE_VERSION(USBVISION_VERSION_STRING);
  150. MODULE_ALIAS(DRIVER_ALIAS);
  151. /*****************************************************************************/
  152. /* SYSFS Code - Copied from the stv680.c usb module. */
  153. /* Device information is located at /sys/class/video4linux/video0 */
  154. /* Device parameters information is located at /sys/module/usbvision */
  155. /* Device USB Information is located at */
  156. /* /sys/bus/usb/drivers/USBVision Video Grabber */
  157. /*****************************************************************************/
  158. #define YES_NO(x) ((x) ? "Yes" : "No")
  159. static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
  160. {
  161. struct video_device *vdev =
  162. container_of(cd, struct video_device, dev);
  163. return video_get_drvdata(vdev);
  164. }
  165. static ssize_t show_version(struct device *cd,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
  169. }
  170. static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  171. static ssize_t show_model(struct device *cd,
  172. struct device_attribute *attr, char *buf)
  173. {
  174. struct video_device *vdev =
  175. container_of(cd, struct video_device, dev);
  176. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  177. return sprintf(buf, "%s\n",
  178. usbvision_device_data[usbvision->DevModel].ModelString);
  179. }
  180. static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  181. static ssize_t show_hue(struct device *cd,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct video_device *vdev =
  185. container_of(cd, struct video_device, dev);
  186. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  187. struct v4l2_control ctrl;
  188. ctrl.id = V4L2_CID_HUE;
  189. ctrl.value = 0;
  190. if(usbvision->user)
  191. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  192. return sprintf(buf, "%d\n", ctrl.value);
  193. }
  194. static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  195. static ssize_t show_contrast(struct device *cd,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct video_device *vdev =
  199. container_of(cd, struct video_device, dev);
  200. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  201. struct v4l2_control ctrl;
  202. ctrl.id = V4L2_CID_CONTRAST;
  203. ctrl.value = 0;
  204. if(usbvision->user)
  205. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  206. return sprintf(buf, "%d\n", ctrl.value);
  207. }
  208. static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  209. static ssize_t show_brightness(struct device *cd,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct video_device *vdev =
  213. container_of(cd, struct video_device, dev);
  214. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  215. struct v4l2_control ctrl;
  216. ctrl.id = V4L2_CID_BRIGHTNESS;
  217. ctrl.value = 0;
  218. if(usbvision->user)
  219. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  220. return sprintf(buf, "%d\n", ctrl.value);
  221. }
  222. static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  223. static ssize_t show_saturation(struct device *cd,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. struct video_device *vdev =
  227. container_of(cd, struct video_device, dev);
  228. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  229. struct v4l2_control ctrl;
  230. ctrl.id = V4L2_CID_SATURATION;
  231. ctrl.value = 0;
  232. if(usbvision->user)
  233. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  234. return sprintf(buf, "%d\n", ctrl.value);
  235. }
  236. static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  237. static ssize_t show_streaming(struct device *cd,
  238. struct device_attribute *attr, char *buf)
  239. {
  240. struct video_device *vdev =
  241. container_of(cd, struct video_device, dev);
  242. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  243. return sprintf(buf, "%s\n",
  244. YES_NO(usbvision->streaming==Stream_On?1:0));
  245. }
  246. static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
  247. static ssize_t show_compression(struct device *cd,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct video_device *vdev =
  251. container_of(cd, struct video_device, dev);
  252. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  253. return sprintf(buf, "%s\n",
  254. YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
  255. }
  256. static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
  257. static ssize_t show_device_bridge(struct device *cd,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct video_device *vdev =
  261. container_of(cd, struct video_device, dev);
  262. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  263. return sprintf(buf, "%d\n", usbvision->bridgeType);
  264. }
  265. static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
  266. static void usbvision_create_sysfs(struct video_device *vdev)
  267. {
  268. int res;
  269. if (!vdev)
  270. return;
  271. do {
  272. res = device_create_file(&vdev->dev, &dev_attr_version);
  273. if (res<0)
  274. break;
  275. res = device_create_file(&vdev->dev, &dev_attr_model);
  276. if (res<0)
  277. break;
  278. res = device_create_file(&vdev->dev, &dev_attr_hue);
  279. if (res<0)
  280. break;
  281. res = device_create_file(&vdev->dev, &dev_attr_contrast);
  282. if (res<0)
  283. break;
  284. res = device_create_file(&vdev->dev, &dev_attr_brightness);
  285. if (res<0)
  286. break;
  287. res = device_create_file(&vdev->dev, &dev_attr_saturation);
  288. if (res<0)
  289. break;
  290. res = device_create_file(&vdev->dev, &dev_attr_streaming);
  291. if (res<0)
  292. break;
  293. res = device_create_file(&vdev->dev, &dev_attr_compression);
  294. if (res<0)
  295. break;
  296. res = device_create_file(&vdev->dev, &dev_attr_bridge);
  297. if (res>=0)
  298. return;
  299. } while (0);
  300. err("%s error: %d\n", __func__, res);
  301. }
  302. static void usbvision_remove_sysfs(struct video_device *vdev)
  303. {
  304. if (vdev) {
  305. device_remove_file(&vdev->dev, &dev_attr_version);
  306. device_remove_file(&vdev->dev, &dev_attr_model);
  307. device_remove_file(&vdev->dev, &dev_attr_hue);
  308. device_remove_file(&vdev->dev, &dev_attr_contrast);
  309. device_remove_file(&vdev->dev, &dev_attr_brightness);
  310. device_remove_file(&vdev->dev, &dev_attr_saturation);
  311. device_remove_file(&vdev->dev, &dev_attr_streaming);
  312. device_remove_file(&vdev->dev, &dev_attr_compression);
  313. device_remove_file(&vdev->dev, &dev_attr_bridge);
  314. }
  315. }
  316. /*
  317. * usbvision_open()
  318. *
  319. * This is part of Video 4 Linux API. The driver can be opened by one
  320. * client only (checks internal counter 'usbvision->user'). The procedure
  321. * then allocates buffers needed for video processing.
  322. *
  323. */
  324. static int usbvision_v4l2_open(struct inode *inode, struct file *file)
  325. {
  326. struct video_device *dev = video_devdata(file);
  327. struct usb_usbvision *usbvision =
  328. (struct usb_usbvision *) video_get_drvdata(dev);
  329. int errCode = 0;
  330. PDEBUG(DBG_IO, "open");
  331. usbvision_reset_powerOffTimer(usbvision);
  332. if (usbvision->user)
  333. errCode = -EBUSY;
  334. else {
  335. /* Allocate memory for the scratch ring buffer */
  336. errCode = usbvision_scratch_alloc(usbvision);
  337. if (isocMode==ISOC_MODE_COMPRESS) {
  338. /* Allocate intermediate decompression buffers
  339. only if needed */
  340. errCode = usbvision_decompress_alloc(usbvision);
  341. }
  342. if (errCode) {
  343. /* Deallocate all buffers if trouble */
  344. usbvision_scratch_free(usbvision);
  345. usbvision_decompress_free(usbvision);
  346. }
  347. }
  348. /* If so far no errors then we shall start the camera */
  349. if (!errCode) {
  350. mutex_lock(&usbvision->lock);
  351. if (usbvision->power == 0) {
  352. usbvision_power_on(usbvision);
  353. usbvision_i2c_register(usbvision);
  354. }
  355. /* Send init sequence only once, it's large! */
  356. if (!usbvision->initialized) {
  357. int setup_ok = 0;
  358. setup_ok = usbvision_setup(usbvision,isocMode);
  359. if (setup_ok)
  360. usbvision->initialized = 1;
  361. else
  362. errCode = -EBUSY;
  363. }
  364. if (!errCode) {
  365. usbvision_begin_streaming(usbvision);
  366. errCode = usbvision_init_isoc(usbvision);
  367. /* device must be initialized before isoc transfer */
  368. usbvision_muxsel(usbvision,0);
  369. usbvision->user++;
  370. } else {
  371. if (PowerOnAtOpen) {
  372. usbvision_i2c_unregister(usbvision);
  373. usbvision_power_off(usbvision);
  374. usbvision->initialized = 0;
  375. }
  376. }
  377. mutex_unlock(&usbvision->lock);
  378. }
  379. /* prepare queues */
  380. usbvision_empty_framequeues(usbvision);
  381. PDEBUG(DBG_IO, "success");
  382. return errCode;
  383. }
  384. /*
  385. * usbvision_v4l2_close()
  386. *
  387. * This is part of Video 4 Linux API. The procedure
  388. * stops streaming and deallocates all buffers that were earlier
  389. * allocated in usbvision_v4l2_open().
  390. *
  391. */
  392. static int usbvision_v4l2_close(struct inode *inode, struct file *file)
  393. {
  394. struct video_device *dev = video_devdata(file);
  395. struct usb_usbvision *usbvision =
  396. (struct usb_usbvision *) video_get_drvdata(dev);
  397. PDEBUG(DBG_IO, "close");
  398. mutex_lock(&usbvision->lock);
  399. usbvision_audio_off(usbvision);
  400. usbvision_restart_isoc(usbvision);
  401. usbvision_stop_isoc(usbvision);
  402. usbvision_decompress_free(usbvision);
  403. usbvision_frames_free(usbvision);
  404. usbvision_empty_framequeues(usbvision);
  405. usbvision_scratch_free(usbvision);
  406. usbvision->user--;
  407. if (PowerOnAtOpen) {
  408. /* power off in a little while
  409. to avoid off/on every close/open short sequences */
  410. usbvision_set_powerOffTimer(usbvision);
  411. usbvision->initialized = 0;
  412. }
  413. mutex_unlock(&usbvision->lock);
  414. if (usbvision->remove_pending) {
  415. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  416. usbvision_release(usbvision);
  417. }
  418. PDEBUG(DBG_IO, "success");
  419. return 0;
  420. }
  421. /*
  422. * usbvision_ioctl()
  423. *
  424. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  425. *
  426. */
  427. #ifdef CONFIG_VIDEO_ADV_DEBUG
  428. static int vidioc_g_register (struct file *file, void *priv,
  429. struct v4l2_register *reg)
  430. {
  431. struct video_device *dev = video_devdata(file);
  432. struct usb_usbvision *usbvision =
  433. (struct usb_usbvision *) video_get_drvdata(dev);
  434. int errCode;
  435. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  436. return -EINVAL;
  437. /* NT100x has a 8-bit register space */
  438. errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
  439. if (errCode < 0) {
  440. err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
  441. __func__, errCode);
  442. return errCode;
  443. }
  444. reg->val = errCode;
  445. return 0;
  446. }
  447. static int vidioc_s_register (struct file *file, void *priv,
  448. struct v4l2_register *reg)
  449. {
  450. struct video_device *dev = video_devdata(file);
  451. struct usb_usbvision *usbvision =
  452. (struct usb_usbvision *) video_get_drvdata(dev);
  453. int errCode;
  454. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  455. return -EINVAL;
  456. /* NT100x has a 8-bit register space */
  457. errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
  458. if (errCode < 0) {
  459. err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
  460. __func__, errCode);
  461. return errCode;
  462. }
  463. return 0;
  464. }
  465. #endif
  466. static int vidioc_querycap (struct file *file, void *priv,
  467. struct v4l2_capability *vc)
  468. {
  469. struct video_device *dev = video_devdata(file);
  470. struct usb_usbvision *usbvision =
  471. (struct usb_usbvision *) video_get_drvdata(dev);
  472. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  473. strlcpy(vc->card,
  474. usbvision_device_data[usbvision->DevModel].ModelString,
  475. sizeof(vc->card));
  476. strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
  477. sizeof(vc->bus_info));
  478. vc->version = USBVISION_DRIVER_VERSION;
  479. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  480. V4L2_CAP_AUDIO |
  481. V4L2_CAP_READWRITE |
  482. V4L2_CAP_STREAMING |
  483. (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  484. return 0;
  485. }
  486. static int vidioc_enum_input (struct file *file, void *priv,
  487. struct v4l2_input *vi)
  488. {
  489. struct video_device *dev = video_devdata(file);
  490. struct usb_usbvision *usbvision =
  491. (struct usb_usbvision *) video_get_drvdata(dev);
  492. int chan;
  493. if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
  494. return -EINVAL;
  495. if (usbvision->have_tuner) {
  496. chan = vi->index;
  497. } else {
  498. chan = vi->index + 1; /*skip Television string*/
  499. }
  500. /* Determine the requested input characteristics
  501. specific for each usbvision card model */
  502. switch(chan) {
  503. case 0:
  504. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  505. strcpy(vi->name, "White Video Input");
  506. } else {
  507. strcpy(vi->name, "Television");
  508. vi->type = V4L2_INPUT_TYPE_TUNER;
  509. vi->audioset = 1;
  510. vi->tuner = chan;
  511. vi->std = USBVISION_NORMS;
  512. }
  513. break;
  514. case 1:
  515. vi->type = V4L2_INPUT_TYPE_CAMERA;
  516. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  517. strcpy(vi->name, "Green Video Input");
  518. } else {
  519. strcpy(vi->name, "Composite Video Input");
  520. }
  521. vi->std = V4L2_STD_PAL;
  522. break;
  523. case 2:
  524. vi->type = V4L2_INPUT_TYPE_CAMERA;
  525. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  526. strcpy(vi->name, "Yellow Video Input");
  527. } else {
  528. strcpy(vi->name, "S-Video Input");
  529. }
  530. vi->std = V4L2_STD_PAL;
  531. break;
  532. case 3:
  533. vi->type = V4L2_INPUT_TYPE_CAMERA;
  534. strcpy(vi->name, "Red Video Input");
  535. vi->std = V4L2_STD_PAL;
  536. break;
  537. }
  538. return 0;
  539. }
  540. static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
  541. {
  542. struct video_device *dev = video_devdata(file);
  543. struct usb_usbvision *usbvision =
  544. (struct usb_usbvision *) video_get_drvdata(dev);
  545. *input = usbvision->ctl_input;
  546. return 0;
  547. }
  548. static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
  549. {
  550. struct video_device *dev = video_devdata(file);
  551. struct usb_usbvision *usbvision =
  552. (struct usb_usbvision *) video_get_drvdata(dev);
  553. if ((input >= usbvision->video_inputs) || (input < 0) )
  554. return -EINVAL;
  555. mutex_lock(&usbvision->lock);
  556. usbvision_muxsel(usbvision, input);
  557. usbvision_set_input(usbvision);
  558. usbvision_set_output(usbvision,
  559. usbvision->curwidth,
  560. usbvision->curheight);
  561. mutex_unlock(&usbvision->lock);
  562. return 0;
  563. }
  564. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
  565. {
  566. struct video_device *dev = video_devdata(file);
  567. struct usb_usbvision *usbvision =
  568. (struct usb_usbvision *) video_get_drvdata(dev);
  569. usbvision->tvnormId=*id;
  570. mutex_lock(&usbvision->lock);
  571. call_i2c_clients(usbvision, VIDIOC_S_STD,
  572. &usbvision->tvnormId);
  573. mutex_unlock(&usbvision->lock);
  574. /* propagate the change to the decoder */
  575. usbvision_muxsel(usbvision, usbvision->ctl_input);
  576. return 0;
  577. }
  578. static int vidioc_g_tuner (struct file *file, void *priv,
  579. struct v4l2_tuner *vt)
  580. {
  581. struct video_device *dev = video_devdata(file);
  582. struct usb_usbvision *usbvision =
  583. (struct usb_usbvision *) video_get_drvdata(dev);
  584. if (!usbvision->have_tuner || vt->index) // Only tuner 0
  585. return -EINVAL;
  586. if(usbvision->radio) {
  587. strcpy(vt->name, "Radio");
  588. vt->type = V4L2_TUNER_RADIO;
  589. } else {
  590. strcpy(vt->name, "Television");
  591. }
  592. /* Let clients fill in the remainder of this struct */
  593. call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
  594. return 0;
  595. }
  596. static int vidioc_s_tuner (struct file *file, void *priv,
  597. struct v4l2_tuner *vt)
  598. {
  599. struct video_device *dev = video_devdata(file);
  600. struct usb_usbvision *usbvision =
  601. (struct usb_usbvision *) video_get_drvdata(dev);
  602. // Only no or one tuner for now
  603. if (!usbvision->have_tuner || vt->index)
  604. return -EINVAL;
  605. /* let clients handle this */
  606. call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
  607. return 0;
  608. }
  609. static int vidioc_g_frequency (struct file *file, void *priv,
  610. struct v4l2_frequency *freq)
  611. {
  612. struct video_device *dev = video_devdata(file);
  613. struct usb_usbvision *usbvision =
  614. (struct usb_usbvision *) video_get_drvdata(dev);
  615. freq->tuner = 0; // Only one tuner
  616. if(usbvision->radio) {
  617. freq->type = V4L2_TUNER_RADIO;
  618. } else {
  619. freq->type = V4L2_TUNER_ANALOG_TV;
  620. }
  621. freq->frequency = usbvision->freq;
  622. return 0;
  623. }
  624. static int vidioc_s_frequency (struct file *file, void *priv,
  625. struct v4l2_frequency *freq)
  626. {
  627. struct video_device *dev = video_devdata(file);
  628. struct usb_usbvision *usbvision =
  629. (struct usb_usbvision *) video_get_drvdata(dev);
  630. // Only no or one tuner for now
  631. if (!usbvision->have_tuner || freq->tuner)
  632. return -EINVAL;
  633. usbvision->freq = freq->frequency;
  634. call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
  635. return 0;
  636. }
  637. static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  638. {
  639. struct video_device *dev = video_devdata(file);
  640. struct usb_usbvision *usbvision =
  641. (struct usb_usbvision *) video_get_drvdata(dev);
  642. memset(a,0,sizeof(*a));
  643. if(usbvision->radio) {
  644. strcpy(a->name,"Radio");
  645. } else {
  646. strcpy(a->name, "TV");
  647. }
  648. return 0;
  649. }
  650. static int vidioc_s_audio (struct file *file, void *fh,
  651. struct v4l2_audio *a)
  652. {
  653. if(a->index) {
  654. return -EINVAL;
  655. }
  656. return 0;
  657. }
  658. static int vidioc_queryctrl (struct file *file, void *priv,
  659. struct v4l2_queryctrl *ctrl)
  660. {
  661. struct video_device *dev = video_devdata(file);
  662. struct usb_usbvision *usbvision =
  663. (struct usb_usbvision *) video_get_drvdata(dev);
  664. int id=ctrl->id;
  665. memset(ctrl,0,sizeof(*ctrl));
  666. ctrl->id=id;
  667. call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
  668. if (!ctrl->type)
  669. return -EINVAL;
  670. return 0;
  671. }
  672. static int vidioc_g_ctrl (struct file *file, void *priv,
  673. struct v4l2_control *ctrl)
  674. {
  675. struct video_device *dev = video_devdata(file);
  676. struct usb_usbvision *usbvision =
  677. (struct usb_usbvision *) video_get_drvdata(dev);
  678. call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
  679. return 0;
  680. }
  681. static int vidioc_s_ctrl (struct file *file, void *priv,
  682. struct v4l2_control *ctrl)
  683. {
  684. struct video_device *dev = video_devdata(file);
  685. struct usb_usbvision *usbvision =
  686. (struct usb_usbvision *) video_get_drvdata(dev);
  687. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  688. return 0;
  689. }
  690. static int vidioc_reqbufs (struct file *file,
  691. void *priv, struct v4l2_requestbuffers *vr)
  692. {
  693. struct video_device *dev = video_devdata(file);
  694. struct usb_usbvision *usbvision =
  695. (struct usb_usbvision *) video_get_drvdata(dev);
  696. int ret;
  697. RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
  698. /* Check input validity:
  699. the user must do a VIDEO CAPTURE and MMAP method. */
  700. if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  701. (vr->memory != V4L2_MEMORY_MMAP))
  702. return -EINVAL;
  703. if(usbvision->streaming == Stream_On) {
  704. if ((ret = usbvision_stream_interrupt(usbvision)))
  705. return ret;
  706. }
  707. usbvision_frames_free(usbvision);
  708. usbvision_empty_framequeues(usbvision);
  709. vr->count = usbvision_frames_alloc(usbvision,vr->count);
  710. usbvision->curFrame = NULL;
  711. return 0;
  712. }
  713. static int vidioc_querybuf (struct file *file,
  714. void *priv, struct v4l2_buffer *vb)
  715. {
  716. struct video_device *dev = video_devdata(file);
  717. struct usb_usbvision *usbvision =
  718. (struct usb_usbvision *) video_get_drvdata(dev);
  719. struct usbvision_frame *frame;
  720. /* FIXME : must control
  721. that buffers are mapped (VIDIOC_REQBUFS has been called) */
  722. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  723. return -EINVAL;
  724. }
  725. if(vb->index>=usbvision->num_frames) {
  726. return -EINVAL;
  727. }
  728. /* Updating the corresponding frame state */
  729. vb->flags = 0;
  730. frame = &usbvision->frame[vb->index];
  731. if(frame->grabstate >= FrameState_Ready)
  732. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  733. if(frame->grabstate >= FrameState_Done)
  734. vb->flags |= V4L2_BUF_FLAG_DONE;
  735. if(frame->grabstate == FrameState_Unused)
  736. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  737. vb->memory = V4L2_MEMORY_MMAP;
  738. vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
  739. vb->memory = V4L2_MEMORY_MMAP;
  740. vb->field = V4L2_FIELD_NONE;
  741. vb->length = usbvision->curwidth*
  742. usbvision->curheight*
  743. usbvision->palette.bytes_per_pixel;
  744. vb->timestamp = usbvision->frame[vb->index].timestamp;
  745. vb->sequence = usbvision->frame[vb->index].sequence;
  746. return 0;
  747. }
  748. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
  749. {
  750. struct video_device *dev = video_devdata(file);
  751. struct usb_usbvision *usbvision =
  752. (struct usb_usbvision *) video_get_drvdata(dev);
  753. struct usbvision_frame *frame;
  754. unsigned long lock_flags;
  755. /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
  756. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  757. return -EINVAL;
  758. }
  759. if(vb->index>=usbvision->num_frames) {
  760. return -EINVAL;
  761. }
  762. frame = &usbvision->frame[vb->index];
  763. if (frame->grabstate != FrameState_Unused) {
  764. return -EAGAIN;
  765. }
  766. /* Mark it as ready and enqueue frame */
  767. frame->grabstate = FrameState_Ready;
  768. frame->scanstate = ScanState_Scanning;
  769. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  770. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  771. /* set v4l2_format index */
  772. frame->v4l2_format = usbvision->palette;
  773. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  774. list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
  775. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  776. return 0;
  777. }
  778. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
  779. {
  780. struct video_device *dev = video_devdata(file);
  781. struct usb_usbvision *usbvision =
  782. (struct usb_usbvision *) video_get_drvdata(dev);
  783. int ret;
  784. struct usbvision_frame *f;
  785. unsigned long lock_flags;
  786. if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  787. return -EINVAL;
  788. if (list_empty(&(usbvision->outqueue))) {
  789. if (usbvision->streaming == Stream_Idle)
  790. return -EINVAL;
  791. ret = wait_event_interruptible
  792. (usbvision->wait_frame,
  793. !list_empty(&(usbvision->outqueue)));
  794. if (ret)
  795. return ret;
  796. }
  797. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  798. f = list_entry(usbvision->outqueue.next,
  799. struct usbvision_frame, frame);
  800. list_del(usbvision->outqueue.next);
  801. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  802. f->grabstate = FrameState_Unused;
  803. vb->memory = V4L2_MEMORY_MMAP;
  804. vb->flags = V4L2_BUF_FLAG_MAPPED |
  805. V4L2_BUF_FLAG_QUEUED |
  806. V4L2_BUF_FLAG_DONE;
  807. vb->index = f->index;
  808. vb->sequence = f->sequence;
  809. vb->timestamp = f->timestamp;
  810. vb->field = V4L2_FIELD_NONE;
  811. vb->bytesused = f->scanlength;
  812. return 0;
  813. }
  814. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  815. {
  816. struct video_device *dev = video_devdata(file);
  817. struct usb_usbvision *usbvision =
  818. (struct usb_usbvision *) video_get_drvdata(dev);
  819. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  820. usbvision->streaming = Stream_On;
  821. call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
  822. return 0;
  823. }
  824. static int vidioc_streamoff(struct file *file,
  825. void *priv, enum v4l2_buf_type type)
  826. {
  827. struct video_device *dev = video_devdata(file);
  828. struct usb_usbvision *usbvision =
  829. (struct usb_usbvision *) video_get_drvdata(dev);
  830. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  831. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  832. return -EINVAL;
  833. if(usbvision->streaming == Stream_On) {
  834. usbvision_stream_interrupt(usbvision);
  835. /* Stop all video streamings */
  836. call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
  837. }
  838. usbvision_empty_framequeues(usbvision);
  839. return 0;
  840. }
  841. static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
  842. struct v4l2_fmtdesc *vfd)
  843. {
  844. if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
  845. return -EINVAL;
  846. }
  847. vfd->flags = 0;
  848. vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  849. strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
  850. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  851. memset(vfd->reserved, 0, sizeof(vfd->reserved));
  852. return 0;
  853. }
  854. static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
  855. struct v4l2_format *vf)
  856. {
  857. struct video_device *dev = video_devdata(file);
  858. struct usb_usbvision *usbvision =
  859. (struct usb_usbvision *) video_get_drvdata(dev);
  860. vf->fmt.pix.width = usbvision->curwidth;
  861. vf->fmt.pix.height = usbvision->curheight;
  862. vf->fmt.pix.pixelformat = usbvision->palette.format;
  863. vf->fmt.pix.bytesperline =
  864. usbvision->curwidth*usbvision->palette.bytes_per_pixel;
  865. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
  866. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  867. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  868. return 0;
  869. }
  870. static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
  871. struct v4l2_format *vf)
  872. {
  873. struct video_device *dev = video_devdata(file);
  874. struct usb_usbvision *usbvision =
  875. (struct usb_usbvision *) video_get_drvdata(dev);
  876. int formatIdx;
  877. /* Find requested format in available ones */
  878. for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
  879. if(vf->fmt.pix.pixelformat ==
  880. usbvision_v4l2_format[formatIdx].format) {
  881. usbvision->palette = usbvision_v4l2_format[formatIdx];
  882. break;
  883. }
  884. }
  885. /* robustness */
  886. if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
  887. return -EINVAL;
  888. }
  889. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  890. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  891. vf->fmt.pix.bytesperline = vf->fmt.pix.width*
  892. usbvision->palette.bytes_per_pixel;
  893. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
  894. return 0;
  895. }
  896. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  897. struct v4l2_format *vf)
  898. {
  899. struct video_device *dev = video_devdata(file);
  900. struct usb_usbvision *usbvision =
  901. (struct usb_usbvision *) video_get_drvdata(dev);
  902. int ret;
  903. if( 0 != (ret=vidioc_try_fmt_vid_cap (file, priv, vf)) ) {
  904. return ret;
  905. }
  906. /* stop io in case it is already in progress */
  907. if(usbvision->streaming == Stream_On) {
  908. if ((ret = usbvision_stream_interrupt(usbvision)))
  909. return ret;
  910. }
  911. usbvision_frames_free(usbvision);
  912. usbvision_empty_framequeues(usbvision);
  913. usbvision->curFrame = NULL;
  914. /* by now we are committed to the new data... */
  915. mutex_lock(&usbvision->lock);
  916. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  917. mutex_unlock(&usbvision->lock);
  918. return 0;
  919. }
  920. static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
  921. size_t count, loff_t *ppos)
  922. {
  923. struct video_device *dev = video_devdata(file);
  924. struct usb_usbvision *usbvision =
  925. (struct usb_usbvision *) video_get_drvdata(dev);
  926. int noblock = file->f_flags & O_NONBLOCK;
  927. unsigned long lock_flags;
  928. int ret,i;
  929. struct usbvision_frame *frame;
  930. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
  931. (unsigned long)count, noblock);
  932. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  933. return -EFAULT;
  934. /* This entry point is compatible with the mmap routines
  935. so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
  936. to get frames or call read on the device. */
  937. if(!usbvision->num_frames) {
  938. /* First, allocate some frames to work with
  939. if this has not been done with VIDIOC_REQBUF */
  940. usbvision_frames_free(usbvision);
  941. usbvision_empty_framequeues(usbvision);
  942. usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
  943. }
  944. if(usbvision->streaming != Stream_On) {
  945. /* no stream is running, make it running ! */
  946. usbvision->streaming = Stream_On;
  947. call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
  948. }
  949. /* Then, enqueue as many frames as possible
  950. (like a user of VIDIOC_QBUF would do) */
  951. for(i=0;i<usbvision->num_frames;i++) {
  952. frame = &usbvision->frame[i];
  953. if(frame->grabstate == FrameState_Unused) {
  954. /* Mark it as ready and enqueue frame */
  955. frame->grabstate = FrameState_Ready;
  956. frame->scanstate = ScanState_Scanning;
  957. /* Accumulated in usbvision_parse_data() */
  958. frame->scanlength = 0;
  959. /* set v4l2_format index */
  960. frame->v4l2_format = usbvision->palette;
  961. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  962. list_add_tail(&frame->frame, &usbvision->inqueue);
  963. spin_unlock_irqrestore(&usbvision->queue_lock,
  964. lock_flags);
  965. }
  966. }
  967. /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
  968. if (list_empty(&(usbvision->outqueue))) {
  969. if(noblock)
  970. return -EAGAIN;
  971. ret = wait_event_interruptible
  972. (usbvision->wait_frame,
  973. !list_empty(&(usbvision->outqueue)));
  974. if (ret)
  975. return ret;
  976. }
  977. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  978. frame = list_entry(usbvision->outqueue.next,
  979. struct usbvision_frame, frame);
  980. list_del(usbvision->outqueue.next);
  981. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  982. /* An error returns an empty frame */
  983. if (frame->grabstate == FrameState_Error) {
  984. frame->bytes_read = 0;
  985. return 0;
  986. }
  987. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
  988. __func__,
  989. frame->index, frame->bytes_read, frame->scanlength);
  990. /* copy bytes to user space; we allow for partials reads */
  991. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  992. count = frame->scanlength - frame->bytes_read;
  993. if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
  994. return -EFAULT;
  995. }
  996. frame->bytes_read += count;
  997. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
  998. __func__,
  999. (unsigned long)count, frame->bytes_read);
  1000. /* For now, forget the frame if it has not been read in one shot. */
  1001. /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
  1002. frame->bytes_read = 0;
  1003. /* Mark it as available to be used again. */
  1004. frame->grabstate = FrameState_Unused;
  1005. /* } */
  1006. return count;
  1007. }
  1008. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1009. {
  1010. unsigned long size = vma->vm_end - vma->vm_start,
  1011. start = vma->vm_start;
  1012. void *pos;
  1013. u32 i;
  1014. struct video_device *dev = video_devdata(file);
  1015. struct usb_usbvision *usbvision =
  1016. (struct usb_usbvision *) video_get_drvdata(dev);
  1017. PDEBUG(DBG_MMAP, "mmap");
  1018. mutex_lock(&usbvision->lock);
  1019. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  1020. mutex_unlock(&usbvision->lock);
  1021. return -EFAULT;
  1022. }
  1023. if (!(vma->vm_flags & VM_WRITE) ||
  1024. size != PAGE_ALIGN(usbvision->max_frame_size)) {
  1025. mutex_unlock(&usbvision->lock);
  1026. return -EINVAL;
  1027. }
  1028. for (i = 0; i < usbvision->num_frames; i++) {
  1029. if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
  1030. vma->vm_pgoff)
  1031. break;
  1032. }
  1033. if (i == usbvision->num_frames) {
  1034. PDEBUG(DBG_MMAP,
  1035. "mmap: user supplied mapping address is out of range");
  1036. mutex_unlock(&usbvision->lock);
  1037. return -EINVAL;
  1038. }
  1039. /* VM_IO is eventually going to replace PageReserved altogether */
  1040. vma->vm_flags |= VM_IO;
  1041. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  1042. pos = usbvision->frame[i].data;
  1043. while (size > 0) {
  1044. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  1045. PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
  1046. mutex_unlock(&usbvision->lock);
  1047. return -EAGAIN;
  1048. }
  1049. start += PAGE_SIZE;
  1050. pos += PAGE_SIZE;
  1051. size -= PAGE_SIZE;
  1052. }
  1053. mutex_unlock(&usbvision->lock);
  1054. return 0;
  1055. }
  1056. /*
  1057. * Here comes the stuff for radio on usbvision based devices
  1058. *
  1059. */
  1060. static int usbvision_radio_open(struct inode *inode, struct file *file)
  1061. {
  1062. struct video_device *dev = video_devdata(file);
  1063. struct usb_usbvision *usbvision =
  1064. (struct usb_usbvision *) video_get_drvdata(dev);
  1065. int errCode = 0;
  1066. PDEBUG(DBG_IO, "%s:", __func__);
  1067. mutex_lock(&usbvision->lock);
  1068. if (usbvision->user) {
  1069. err("%s: Someone tried to open an already opened USBVision Radio!", __func__);
  1070. errCode = -EBUSY;
  1071. }
  1072. else {
  1073. if(PowerOnAtOpen) {
  1074. usbvision_reset_powerOffTimer(usbvision);
  1075. if (usbvision->power == 0) {
  1076. usbvision_power_on(usbvision);
  1077. usbvision_i2c_register(usbvision);
  1078. }
  1079. }
  1080. /* Alternate interface 1 is is the biggest frame size */
  1081. errCode = usbvision_set_alternate(usbvision);
  1082. if (errCode < 0) {
  1083. usbvision->last_error = errCode;
  1084. errCode = -EBUSY;
  1085. goto out;
  1086. }
  1087. // If so far no errors then we shall start the radio
  1088. usbvision->radio = 1;
  1089. call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
  1090. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  1091. usbvision->user++;
  1092. }
  1093. if (errCode) {
  1094. if (PowerOnAtOpen) {
  1095. usbvision_i2c_unregister(usbvision);
  1096. usbvision_power_off(usbvision);
  1097. usbvision->initialized = 0;
  1098. }
  1099. }
  1100. out:
  1101. mutex_unlock(&usbvision->lock);
  1102. return errCode;
  1103. }
  1104. static int usbvision_radio_close(struct inode *inode, struct file *file)
  1105. {
  1106. struct video_device *dev = video_devdata(file);
  1107. struct usb_usbvision *usbvision =
  1108. (struct usb_usbvision *) video_get_drvdata(dev);
  1109. int errCode = 0;
  1110. PDEBUG(DBG_IO, "");
  1111. mutex_lock(&usbvision->lock);
  1112. /* Set packet size to 0 */
  1113. usbvision->ifaceAlt=0;
  1114. errCode = usb_set_interface(usbvision->dev, usbvision->iface,
  1115. usbvision->ifaceAlt);
  1116. usbvision_audio_off(usbvision);
  1117. usbvision->radio=0;
  1118. usbvision->user--;
  1119. if (PowerOnAtOpen) {
  1120. usbvision_set_powerOffTimer(usbvision);
  1121. usbvision->initialized = 0;
  1122. }
  1123. mutex_unlock(&usbvision->lock);
  1124. if (usbvision->remove_pending) {
  1125. printk(KERN_INFO "%s: Final disconnect\n", __func__);
  1126. usbvision_release(usbvision);
  1127. }
  1128. PDEBUG(DBG_IO, "success");
  1129. return errCode;
  1130. }
  1131. /*
  1132. * Here comes the stuff for vbi on usbvision based devices
  1133. *
  1134. */
  1135. static int usbvision_vbi_open(struct inode *inode, struct file *file)
  1136. {
  1137. /* TODO */
  1138. return -ENODEV;
  1139. }
  1140. static int usbvision_vbi_close(struct inode *inode, struct file *file)
  1141. {
  1142. /* TODO */
  1143. return -ENODEV;
  1144. }
  1145. static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
  1146. unsigned int cmd, void *arg)
  1147. {
  1148. /* TODO */
  1149. return -ENOIOCTLCMD;
  1150. }
  1151. static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
  1152. unsigned int cmd, unsigned long arg)
  1153. {
  1154. return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
  1155. }
  1156. //
  1157. // Video registration stuff
  1158. //
  1159. // Video template
  1160. static const struct file_operations usbvision_fops = {
  1161. .owner = THIS_MODULE,
  1162. .open = usbvision_v4l2_open,
  1163. .release = usbvision_v4l2_close,
  1164. .read = usbvision_v4l2_read,
  1165. .mmap = usbvision_v4l2_mmap,
  1166. .ioctl = video_ioctl2,
  1167. .llseek = no_llseek,
  1168. /* .poll = video_poll, */
  1169. .compat_ioctl = v4l_compat_ioctl32,
  1170. };
  1171. static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
  1172. .vidioc_querycap = vidioc_querycap,
  1173. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1174. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1175. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1176. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1177. .vidioc_reqbufs = vidioc_reqbufs,
  1178. .vidioc_querybuf = vidioc_querybuf,
  1179. .vidioc_qbuf = vidioc_qbuf,
  1180. .vidioc_dqbuf = vidioc_dqbuf,
  1181. .vidioc_s_std = vidioc_s_std,
  1182. .vidioc_enum_input = vidioc_enum_input,
  1183. .vidioc_g_input = vidioc_g_input,
  1184. .vidioc_s_input = vidioc_s_input,
  1185. .vidioc_queryctrl = vidioc_queryctrl,
  1186. .vidioc_g_audio = vidioc_g_audio,
  1187. .vidioc_s_audio = vidioc_s_audio,
  1188. .vidioc_g_ctrl = vidioc_g_ctrl,
  1189. .vidioc_s_ctrl = vidioc_s_ctrl,
  1190. .vidioc_streamon = vidioc_streamon,
  1191. .vidioc_streamoff = vidioc_streamoff,
  1192. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1193. /* .vidiocgmbuf = vidiocgmbuf, */
  1194. #endif
  1195. .vidioc_g_tuner = vidioc_g_tuner,
  1196. .vidioc_s_tuner = vidioc_s_tuner,
  1197. .vidioc_g_frequency = vidioc_g_frequency,
  1198. .vidioc_s_frequency = vidioc_s_frequency,
  1199. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1200. .vidioc_g_register = vidioc_g_register,
  1201. .vidioc_s_register = vidioc_s_register,
  1202. #endif
  1203. };
  1204. static struct video_device usbvision_video_template = {
  1205. .owner = THIS_MODULE,
  1206. .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
  1207. .fops = &usbvision_fops,
  1208. .ioctl_ops = &usbvision_ioctl_ops,
  1209. .name = "usbvision-video",
  1210. .release = video_device_release,
  1211. .minor = -1,
  1212. .tvnorms = USBVISION_NORMS,
  1213. .current_norm = V4L2_STD_PAL
  1214. };
  1215. // Radio template
  1216. static const struct file_operations usbvision_radio_fops = {
  1217. .owner = THIS_MODULE,
  1218. .open = usbvision_radio_open,
  1219. .release = usbvision_radio_close,
  1220. .ioctl = video_ioctl2,
  1221. .llseek = no_llseek,
  1222. .compat_ioctl = v4l_compat_ioctl32,
  1223. };
  1224. static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
  1225. .vidioc_querycap = vidioc_querycap,
  1226. .vidioc_enum_input = vidioc_enum_input,
  1227. .vidioc_g_input = vidioc_g_input,
  1228. .vidioc_s_input = vidioc_s_input,
  1229. .vidioc_queryctrl = vidioc_queryctrl,
  1230. .vidioc_g_audio = vidioc_g_audio,
  1231. .vidioc_s_audio = vidioc_s_audio,
  1232. .vidioc_g_ctrl = vidioc_g_ctrl,
  1233. .vidioc_s_ctrl = vidioc_s_ctrl,
  1234. .vidioc_g_tuner = vidioc_g_tuner,
  1235. .vidioc_s_tuner = vidioc_s_tuner,
  1236. .vidioc_g_frequency = vidioc_g_frequency,
  1237. .vidioc_s_frequency = vidioc_s_frequency,
  1238. };
  1239. static struct video_device usbvision_radio_template = {
  1240. .owner = THIS_MODULE,
  1241. .type = VID_TYPE_TUNER,
  1242. .fops = &usbvision_radio_fops,
  1243. .name = "usbvision-radio",
  1244. .release = video_device_release,
  1245. .minor = -1,
  1246. .ioctl_ops = &usbvision_radio_ioctl_ops,
  1247. .tvnorms = USBVISION_NORMS,
  1248. .current_norm = V4L2_STD_PAL
  1249. };
  1250. // vbi template
  1251. static const struct file_operations usbvision_vbi_fops = {
  1252. .owner = THIS_MODULE,
  1253. .open = usbvision_vbi_open,
  1254. .release = usbvision_vbi_close,
  1255. .ioctl = usbvision_vbi_ioctl,
  1256. .llseek = no_llseek,
  1257. .compat_ioctl = v4l_compat_ioctl32,
  1258. };
  1259. static struct video_device usbvision_vbi_template=
  1260. {
  1261. .owner = THIS_MODULE,
  1262. .type = VID_TYPE_TUNER,
  1263. .fops = &usbvision_vbi_fops,
  1264. .release = video_device_release,
  1265. .name = "usbvision-vbi",
  1266. .minor = -1,
  1267. };
  1268. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  1269. struct video_device *vdev_template,
  1270. char *name)
  1271. {
  1272. struct usb_device *usb_dev = usbvision->dev;
  1273. struct video_device *vdev;
  1274. if (usb_dev == NULL) {
  1275. err("%s: usbvision->dev is not set", __func__);
  1276. return NULL;
  1277. }
  1278. vdev = video_device_alloc();
  1279. if (NULL == vdev) {
  1280. return NULL;
  1281. }
  1282. *vdev = *vdev_template;
  1283. // vdev->minor = -1;
  1284. vdev->parent = &usb_dev->dev;
  1285. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1286. video_set_drvdata(vdev, usbvision);
  1287. return vdev;
  1288. }
  1289. // unregister video4linux devices
  1290. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1291. {
  1292. // vbi Device:
  1293. if (usbvision->vbi) {
  1294. PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
  1295. usbvision->vbi->minor & 0x1f);
  1296. if (usbvision->vbi->minor != -1) {
  1297. video_unregister_device(usbvision->vbi);
  1298. } else {
  1299. video_device_release(usbvision->vbi);
  1300. }
  1301. usbvision->vbi = NULL;
  1302. }
  1303. // Radio Device:
  1304. if (usbvision->rdev) {
  1305. PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
  1306. usbvision->rdev->minor & 0x1f);
  1307. if (usbvision->rdev->minor != -1) {
  1308. video_unregister_device(usbvision->rdev);
  1309. } else {
  1310. video_device_release(usbvision->rdev);
  1311. }
  1312. usbvision->rdev = NULL;
  1313. }
  1314. // Video Device:
  1315. if (usbvision->vdev) {
  1316. PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
  1317. usbvision->vdev->minor & 0x1f);
  1318. if (usbvision->vdev->minor != -1) {
  1319. video_unregister_device(usbvision->vdev);
  1320. } else {
  1321. video_device_release(usbvision->vdev);
  1322. }
  1323. usbvision->vdev = NULL;
  1324. }
  1325. }
  1326. // register video4linux devices
  1327. static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
  1328. {
  1329. // Video Device:
  1330. usbvision->vdev = usbvision_vdev_init(usbvision,
  1331. &usbvision_video_template,
  1332. "USBVision Video");
  1333. if (usbvision->vdev == NULL) {
  1334. goto err_exit;
  1335. }
  1336. if (video_register_device(usbvision->vdev,
  1337. VFL_TYPE_GRABBER,
  1338. video_nr)<0) {
  1339. goto err_exit;
  1340. }
  1341. printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
  1342. usbvision->nr,usbvision->vdev->minor & 0x1f);
  1343. // Radio Device:
  1344. if (usbvision_device_data[usbvision->DevModel].Radio) {
  1345. // usbvision has radio
  1346. usbvision->rdev = usbvision_vdev_init(usbvision,
  1347. &usbvision_radio_template,
  1348. "USBVision Radio");
  1349. if (usbvision->rdev == NULL) {
  1350. goto err_exit;
  1351. }
  1352. if (video_register_device(usbvision->rdev,
  1353. VFL_TYPE_RADIO,
  1354. radio_nr)<0) {
  1355. goto err_exit;
  1356. }
  1357. printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
  1358. usbvision->nr, usbvision->rdev->minor & 0x1f);
  1359. }
  1360. // vbi Device:
  1361. if (usbvision_device_data[usbvision->DevModel].vbi) {
  1362. usbvision->vbi = usbvision_vdev_init(usbvision,
  1363. &usbvision_vbi_template,
  1364. "USBVision VBI");
  1365. if (usbvision->vdev == NULL) {
  1366. goto err_exit;
  1367. }
  1368. if (video_register_device(usbvision->vbi,
  1369. VFL_TYPE_VBI,
  1370. vbi_nr)<0) {
  1371. goto err_exit;
  1372. }
  1373. printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
  1374. usbvision->nr,usbvision->vbi->minor & 0x1f);
  1375. }
  1376. // all done
  1377. return 0;
  1378. err_exit:
  1379. err("USBVision[%d]: video_register_device() failed", usbvision->nr);
  1380. usbvision_unregister_video(usbvision);
  1381. return -1;
  1382. }
  1383. /*
  1384. * usbvision_alloc()
  1385. *
  1386. * This code allocates the struct usb_usbvision.
  1387. * It is filled with default values.
  1388. *
  1389. * Returns NULL on error, a pointer to usb_usbvision else.
  1390. *
  1391. */
  1392. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
  1393. {
  1394. struct usb_usbvision *usbvision;
  1395. if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
  1396. NULL) {
  1397. goto err_exit;
  1398. }
  1399. usbvision->dev = dev;
  1400. mutex_init(&usbvision->lock); /* available */
  1401. // prepare control urb for control messages during interrupts
  1402. usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1403. if (usbvision->ctrlUrb == NULL) {
  1404. goto err_exit;
  1405. }
  1406. init_waitqueue_head(&usbvision->ctrlUrb_wq);
  1407. usbvision_init_powerOffTimer(usbvision);
  1408. return usbvision;
  1409. err_exit:
  1410. if (usbvision && usbvision->ctrlUrb) {
  1411. usb_free_urb(usbvision->ctrlUrb);
  1412. }
  1413. if (usbvision) {
  1414. kfree(usbvision);
  1415. }
  1416. return NULL;
  1417. }
  1418. /*
  1419. * usbvision_release()
  1420. *
  1421. * This code does final release of struct usb_usbvision. This happens
  1422. * after the device is disconnected -and- all clients closed their files.
  1423. *
  1424. */
  1425. static void usbvision_release(struct usb_usbvision *usbvision)
  1426. {
  1427. PDEBUG(DBG_PROBE, "");
  1428. mutex_lock(&usbvision->lock);
  1429. usbvision_reset_powerOffTimer(usbvision);
  1430. usbvision->initialized = 0;
  1431. mutex_unlock(&usbvision->lock);
  1432. usbvision_remove_sysfs(usbvision->vdev);
  1433. usbvision_unregister_video(usbvision);
  1434. if (usbvision->ctrlUrb) {
  1435. usb_free_urb(usbvision->ctrlUrb);
  1436. }
  1437. kfree(usbvision);
  1438. PDEBUG(DBG_PROBE, "success");
  1439. }
  1440. /*********************** usb interface **********************************/
  1441. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1442. {
  1443. int model;
  1444. if (usbvision == NULL)
  1445. return;
  1446. model = usbvision->DevModel;
  1447. usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
  1448. if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
  1449. usbvision->Vin_Reg2_Preset =
  1450. usbvision_device_data[usbvision->DevModel].Vin_Reg2;
  1451. } else {
  1452. usbvision->Vin_Reg2_Preset = 0;
  1453. }
  1454. usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
  1455. usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
  1456. usbvision->ctl_input = 0;
  1457. /* This should be here to make i2c clients to be able to register */
  1458. /* first switch off audio */
  1459. usbvision_audio_off(usbvision);
  1460. if (!PowerOnAtOpen) {
  1461. /* and then power up the noisy tuner */
  1462. usbvision_power_on(usbvision);
  1463. usbvision_i2c_register(usbvision);
  1464. }
  1465. }
  1466. /*
  1467. * usbvision_probe()
  1468. *
  1469. * This procedure queries device descriptor and accepts the interface
  1470. * if it looks like USBVISION video device
  1471. *
  1472. */
  1473. static int __devinit usbvision_probe(struct usb_interface *intf,
  1474. const struct usb_device_id *devid)
  1475. {
  1476. struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
  1477. struct usb_interface *uif;
  1478. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1479. const struct usb_host_interface *interface;
  1480. struct usb_usbvision *usbvision = NULL;
  1481. const struct usb_endpoint_descriptor *endpoint;
  1482. int model,i;
  1483. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1484. dev->descriptor.idVendor,
  1485. dev->descriptor.idProduct, ifnum);
  1486. model = devid->driver_info;
  1487. if ( (model<0) || (model>=usbvision_device_data_size) ) {
  1488. PDEBUG(DBG_PROBE, "model out of bounds %d",model);
  1489. return -ENODEV;
  1490. }
  1491. printk(KERN_INFO "%s: %s found\n", __func__,
  1492. usbvision_device_data[model].ModelString);
  1493. if (usbvision_device_data[model].Interface >= 0) {
  1494. interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
  1495. } else {
  1496. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1497. }
  1498. endpoint = &interface->endpoint[1].desc;
  1499. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1500. USB_ENDPOINT_XFER_ISOC) {
  1501. err("%s: interface %d. has non-ISO endpoint!",
  1502. __func__, ifnum);
  1503. err("%s: Endpoint attributes %d",
  1504. __func__, endpoint->bmAttributes);
  1505. return -ENODEV;
  1506. }
  1507. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
  1508. USB_DIR_OUT) {
  1509. err("%s: interface %d. has ISO OUT endpoint!",
  1510. __func__, ifnum);
  1511. return -ENODEV;
  1512. }
  1513. if ((usbvision = usbvision_alloc(dev)) == NULL) {
  1514. err("%s: couldn't allocate USBVision struct", __func__);
  1515. return -ENOMEM;
  1516. }
  1517. if (dev->descriptor.bNumConfigurations > 1) {
  1518. usbvision->bridgeType = BRIDGE_NT1004;
  1519. } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
  1520. usbvision->bridgeType = BRIDGE_NT1005;
  1521. } else {
  1522. usbvision->bridgeType = BRIDGE_NT1003;
  1523. }
  1524. PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
  1525. mutex_lock(&usbvision->lock);
  1526. /* compute alternate max packet sizes */
  1527. uif = dev->actconfig->interface[0];
  1528. usbvision->num_alt=uif->num_altsetting;
  1529. PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
  1530. usbvision->alt_max_pkt_size = kmalloc(32*
  1531. usbvision->num_alt,GFP_KERNEL);
  1532. if (usbvision->alt_max_pkt_size == NULL) {
  1533. err("usbvision: out of memory!\n");
  1534. mutex_unlock(&usbvision->lock);
  1535. return -ENOMEM;
  1536. }
  1537. for (i = 0; i < usbvision->num_alt ; i++) {
  1538. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1539. wMaxPacketSize);
  1540. usbvision->alt_max_pkt_size[i] =
  1541. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1542. PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
  1543. usbvision->alt_max_pkt_size[i]);
  1544. }
  1545. usbvision->nr = usbvision_nr++;
  1546. usbvision->have_tuner = usbvision_device_data[model].Tuner;
  1547. if (usbvision->have_tuner) {
  1548. usbvision->tuner_type = usbvision_device_data[model].TunerType;
  1549. }
  1550. usbvision->tuner_addr = ADDR_UNSET;
  1551. usbvision->DevModel = model;
  1552. usbvision->remove_pending = 0;
  1553. usbvision->iface = ifnum;
  1554. usbvision->ifaceAlt = 0;
  1555. usbvision->video_endp = endpoint->bEndpointAddress;
  1556. usbvision->isocPacketSize = 0;
  1557. usbvision->usb_bandwidth = 0;
  1558. usbvision->user = 0;
  1559. usbvision->streaming = Stream_Off;
  1560. usbvision_register_video(usbvision);
  1561. usbvision_configure_video(usbvision);
  1562. mutex_unlock(&usbvision->lock);
  1563. usb_set_intfdata (intf, usbvision);
  1564. usbvision_create_sysfs(usbvision->vdev);
  1565. PDEBUG(DBG_PROBE, "success");
  1566. return 0;
  1567. }
  1568. /*
  1569. * usbvision_disconnect()
  1570. *
  1571. * This procedure stops all driver activity, deallocates interface-private
  1572. * structure (pointed by 'ptr') and after that driver should be removable
  1573. * with no ill consequences.
  1574. *
  1575. */
  1576. static void __devexit usbvision_disconnect(struct usb_interface *intf)
  1577. {
  1578. struct usb_usbvision *usbvision = usb_get_intfdata(intf);
  1579. PDEBUG(DBG_PROBE, "");
  1580. if (usbvision == NULL) {
  1581. err("%s: usb_get_intfdata() failed", __func__);
  1582. return;
  1583. }
  1584. usb_set_intfdata (intf, NULL);
  1585. mutex_lock(&usbvision->lock);
  1586. // At this time we ask to cancel outstanding URBs
  1587. usbvision_stop_isoc(usbvision);
  1588. if (usbvision->power) {
  1589. usbvision_i2c_unregister(usbvision);
  1590. usbvision_power_off(usbvision);
  1591. }
  1592. usbvision->remove_pending = 1; // Now all ISO data will be ignored
  1593. usb_put_dev(usbvision->dev);
  1594. usbvision->dev = NULL; // USB device is no more
  1595. mutex_unlock(&usbvision->lock);
  1596. if (usbvision->user) {
  1597. printk(KERN_INFO "%s: In use, disconnect pending\n",
  1598. __func__);
  1599. wake_up_interruptible(&usbvision->wait_frame);
  1600. wake_up_interruptible(&usbvision->wait_stream);
  1601. } else {
  1602. usbvision_release(usbvision);
  1603. }
  1604. PDEBUG(DBG_PROBE, "success");
  1605. }
  1606. static struct usb_driver usbvision_driver = {
  1607. .name = "usbvision",
  1608. .id_table = usbvision_table,
  1609. .probe = usbvision_probe,
  1610. .disconnect = usbvision_disconnect
  1611. };
  1612. /*
  1613. * usbvision_init()
  1614. *
  1615. * This code is run to initialize the driver.
  1616. *
  1617. */
  1618. static int __init usbvision_init(void)
  1619. {
  1620. int errCode;
  1621. PDEBUG(DBG_PROBE, "");
  1622. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1623. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1624. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1625. /* disable planar mode support unless compression enabled */
  1626. if (isocMode != ISOC_MODE_COMPRESS ) {
  1627. // FIXME : not the right way to set supported flag
  1628. usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
  1629. usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
  1630. }
  1631. errCode = usb_register(&usbvision_driver);
  1632. if (errCode == 0) {
  1633. printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
  1634. PDEBUG(DBG_PROBE, "success");
  1635. }
  1636. return errCode;
  1637. }
  1638. static void __exit usbvision_exit(void)
  1639. {
  1640. PDEBUG(DBG_PROBE, "");
  1641. usb_deregister(&usbvision_driver);
  1642. PDEBUG(DBG_PROBE, "success");
  1643. }
  1644. module_init(usbvision_init);
  1645. module_exit(usbvision_exit);
  1646. /*
  1647. * Overrides for Emacs so that we follow Linus's tabbing style.
  1648. * ---------------------------------------------------------------------------
  1649. * Local variables:
  1650. * c-basic-offset: 8
  1651. * End:
  1652. */