usbvision-video.c 46 KB

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