usbvision-video.c 53 KB

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