usbvision-video.c 47 KB

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