usbvision-video.c 52 KB

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