usbvision-video.c 47 KB

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