usbvision-video.c 53 KB

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