usbvision-video.c 47 KB

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