stv680.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
  3. *
  4. * Thanks to STMicroelectronics for information on the usb commands, and
  5. * to Steve Miller at STM for his help and encouragement while I was
  6. * writing this driver.
  7. *
  8. * This driver is based heavily on the
  9. * Endpoints (formerly known as AOX) se401 USB Camera Driver
  10. * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
  11. *
  12. * Still somewhat based on the Linux ov511 driver.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  21. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * History:
  29. * ver 0.1 October, 2001. Initial attempt.
  30. *
  31. * ver 0.2 November, 2001. Fixed asbility to resize, added brightness
  32. * function, made more stable (?)
  33. *
  34. * ver 0.21 Nov, 2001. Added gamma correction and white balance,
  35. * due to Alexander Schwartz. Still trying to
  36. * improve stablility. Moved stuff into stv680.h
  37. *
  38. * ver 0.22 Nov, 2001. Added sharpen function (by Michael Sweet,
  39. * mike@easysw.com) from GIMP, also used in pencam.
  40. * Simple, fast, good integer math routine.
  41. *
  42. * ver 0.23 Dec, 2001 (gkh)
  43. * Took out sharpen function, ran code through
  44. * Lindent, and did other minor tweaks to get
  45. * things to work properly with 2.5.1
  46. *
  47. * ver 0.24 Jan, 2002 (kjs)
  48. * Fixed the problem with webcam crashing after
  49. * two pictures. Changed the way pic is halved to
  50. * improve quality. Got rid of green line around
  51. * frame. Fix brightness reset when changing size
  52. * bug. Adjusted gamma filters slightly.
  53. *
  54. * ver 0.25 Jan, 2002 (kjs)
  55. * Fixed a bug in which the driver sometimes attempted
  56. * to set to a non-supported size. This allowed
  57. * gnomemeeting to work.
  58. * Fixed proc entry removal bug.
  59. */
  60. #include <linux/module.h>
  61. #include <linux/init.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/slab.h>
  64. #include <linux/pagemap.h>
  65. #include <linux/errno.h>
  66. #include <linux/videodev.h>
  67. #include <media/v4l2-common.h>
  68. #include <media/v4l2-ioctl.h>
  69. #include <linux/usb.h>
  70. #include <linux/mutex.h>
  71. #include "stv680.h"
  72. static int video_nr = -1;
  73. static int swapRGB; /* 0 = default for auto select */
  74. /* 0 = default to allow auto select; -1 = swap never, +1 = swap always */
  75. static int swapRGB_on;
  76. static unsigned int debug;
  77. #define PDEBUG(level, fmt, args...) \
  78. do { \
  79. if (debug >= level) \
  80. info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
  81. } while (0)
  82. /*
  83. * Version Information
  84. */
  85. #define DRIVER_VERSION "v0.25"
  86. #define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
  87. #define DRIVER_DESC "STV0680 USB Camera Driver"
  88. MODULE_AUTHOR (DRIVER_AUTHOR);
  89. MODULE_DESCRIPTION (DRIVER_DESC);
  90. MODULE_LICENSE ("GPL");
  91. module_param(debug, int, S_IRUGO | S_IWUSR);
  92. MODULE_PARM_DESC (debug, "Debug enabled or not");
  93. module_param(swapRGB_on, int, 0);
  94. MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
  95. module_param(video_nr, int, 0);
  96. /********************************************************************
  97. *
  98. * Memory management
  99. *
  100. * This is a shameless copy from the USB-cpia driver (linux kernel
  101. * version 2.3.29 or so, I have no idea what this code actually does ;).
  102. * Actually it seems to be a copy of a shameless copy of the bttv-driver.
  103. * Or that is a copy of a shameless copy of ... (To the powers: is there
  104. * no generic kernel-function to do this sort of stuff?)
  105. *
  106. * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
  107. * there will be one, but apparentely not yet -jerdfelt
  108. *
  109. * So I copied it again for the ov511 driver -claudio
  110. *
  111. * Same for the se401 driver -Jeroen
  112. *
  113. * And the STV0680 driver - Kevin
  114. ********************************************************************/
  115. static void *rvmalloc (unsigned long size)
  116. {
  117. void *mem;
  118. unsigned long adr;
  119. size = PAGE_ALIGN(size);
  120. mem = vmalloc_32 (size);
  121. if (!mem)
  122. return NULL;
  123. memset (mem, 0, size); /* Clear the ram out, no junk to the user */
  124. adr = (unsigned long) mem;
  125. while (size > 0) {
  126. SetPageReserved(vmalloc_to_page((void *)adr));
  127. adr += PAGE_SIZE;
  128. size -= PAGE_SIZE;
  129. }
  130. return mem;
  131. }
  132. static void rvfree (void *mem, unsigned long size)
  133. {
  134. unsigned long adr;
  135. if (!mem)
  136. return;
  137. adr = (unsigned long) mem;
  138. while ((long) size > 0) {
  139. ClearPageReserved(vmalloc_to_page((void *)adr));
  140. adr += PAGE_SIZE;
  141. size -= PAGE_SIZE;
  142. }
  143. vfree (mem);
  144. }
  145. /*********************************************************************
  146. * pencam read/write functions
  147. ********************************************************************/
  148. static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
  149. {
  150. int ret = -1;
  151. switch (set) {
  152. case 0: /* 0xc1 */
  153. ret = usb_control_msg (stv680->udev,
  154. usb_rcvctrlpipe (stv680->udev, 0),
  155. req,
  156. (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  157. value, 0, buffer, size, PENCAM_TIMEOUT);
  158. break;
  159. case 1: /* 0x41 */
  160. ret = usb_control_msg (stv680->udev,
  161. usb_sndctrlpipe (stv680->udev, 0),
  162. req,
  163. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  164. value, 0, buffer, size, PENCAM_TIMEOUT);
  165. break;
  166. case 2: /* 0x80 */
  167. ret = usb_control_msg (stv680->udev,
  168. usb_rcvctrlpipe (stv680->udev, 0),
  169. req,
  170. (USB_DIR_IN | USB_RECIP_DEVICE),
  171. value, 0, buffer, size, PENCAM_TIMEOUT);
  172. break;
  173. case 3: /* 0x40 */
  174. ret = usb_control_msg (stv680->udev,
  175. usb_sndctrlpipe (stv680->udev, 0),
  176. req,
  177. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
  178. value, 0, buffer, size, PENCAM_TIMEOUT);
  179. break;
  180. }
  181. if ((ret < 0) && (req != 0x0a)) {
  182. PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
  183. }
  184. return ret;
  185. }
  186. static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
  187. {
  188. if (configuration != dev->udev->actconfig->desc.bConfigurationValue
  189. || usb_reset_configuration (dev->udev) < 0) {
  190. PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
  191. return -1;
  192. }
  193. if (usb_set_interface (dev->udev, interface, alternate) < 0) {
  194. PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
  195. return -1;
  196. }
  197. return 0;
  198. }
  199. static int stv_stop_video (struct usb_stv *dev)
  200. {
  201. int i;
  202. unsigned char *buf;
  203. buf = kmalloc (40, GFP_KERNEL);
  204. if (buf == NULL) {
  205. PDEBUG (0, "STV(e): Out of (small buf) memory");
  206. return -1;
  207. }
  208. /* this is a high priority command; it stops all lower order commands */
  209. if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
  210. i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
  211. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
  212. } else {
  213. PDEBUG (1, "STV(i): Camera reset to idle mode.");
  214. }
  215. if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
  216. PDEBUG (1, "STV(e): Reset config during exit failed");
  217. /* get current mode */
  218. buf[0] = 0xf0;
  219. if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
  220. PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
  221. if (dev->origMode != buf[0]) {
  222. memset (buf, 0, 8);
  223. buf[0] = (unsigned char) dev->origMode;
  224. if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
  225. PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
  226. i = -1;
  227. }
  228. buf[0] = 0xf0;
  229. i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
  230. if ((i != 0x08) || (buf[0] != dev->origMode)) {
  231. PDEBUG (0, "STV(e): camera NOT set to original resolution.");
  232. i = -1;
  233. } else
  234. PDEBUG (0, "STV(i): Camera set to original resolution");
  235. }
  236. /* origMode */
  237. kfree(buf);
  238. return i;
  239. }
  240. static int stv_set_video_mode (struct usb_stv *dev)
  241. {
  242. int i, stop_video = 1;
  243. unsigned char *buf;
  244. buf = kmalloc (40, GFP_KERNEL);
  245. if (buf == NULL) {
  246. PDEBUG (0, "STV(e): Out of (small buf) memory");
  247. return -1;
  248. }
  249. if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
  250. kfree(buf);
  251. return i;
  252. }
  253. i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
  254. if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
  255. PDEBUG (1, "STV(e): Could not get descriptor 0100.");
  256. goto error;
  257. }
  258. /* set alternate interface 1 */
  259. if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
  260. goto error;
  261. if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
  262. goto error;
  263. PDEBUG (1, "STV(i): Setting video mode.");
  264. /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
  265. if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
  266. stop_video = 0;
  267. goto error;
  268. }
  269. goto exit;
  270. error:
  271. kfree(buf);
  272. if (stop_video == 1)
  273. stv_stop_video (dev);
  274. return -1;
  275. exit:
  276. kfree(buf);
  277. return 0;
  278. }
  279. static int stv_init (struct usb_stv *stv680)
  280. {
  281. int i = 0;
  282. unsigned char *buffer;
  283. unsigned long int bufsize;
  284. buffer = kzalloc (40, GFP_KERNEL);
  285. if (buffer == NULL) {
  286. PDEBUG (0, "STV(e): Out of (small buf) memory");
  287. return -1;
  288. }
  289. udelay (100);
  290. /* set config 1, interface 0, alternate 0 */
  291. if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
  292. kfree(buffer);
  293. PDEBUG (0, "STV(e): set config 1,0,0 failed");
  294. return -1;
  295. }
  296. /* ping camera to be sure STV0680 is present */
  297. if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
  298. goto error;
  299. if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
  300. PDEBUG (1, "STV(e): camera ping failed!!");
  301. goto error;
  302. }
  303. /* get camera descriptor */
  304. if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
  305. goto error;
  306. i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
  307. if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
  308. PDEBUG (1, "STV(e): Could not get descriptor 0200.");
  309. goto error;
  310. }
  311. if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
  312. goto error;
  313. if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
  314. goto error;
  315. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  316. goto error;
  317. stv680->SupportedModes = buffer[7];
  318. i = stv680->SupportedModes;
  319. stv680->CIF = 0;
  320. stv680->VGA = 0;
  321. stv680->QVGA = 0;
  322. if (i & 1)
  323. stv680->CIF = 1;
  324. if (i & 2)
  325. stv680->VGA = 1;
  326. if (i & 8)
  327. stv680->QVGA = 1;
  328. if (stv680->SupportedModes == 0) {
  329. PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
  330. i = -1;
  331. goto error;
  332. } else {
  333. if (stv680->CIF)
  334. PDEBUG (0, "STV(i): CIF is supported");
  335. if (stv680->QVGA)
  336. PDEBUG (0, "STV(i): QVGA is supported");
  337. }
  338. /* FW rev, ASIC rev, sensor ID */
  339. PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
  340. PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
  341. PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
  342. /* set alternate interface 1 */
  343. if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
  344. goto error;
  345. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  346. goto error;
  347. if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
  348. goto error;
  349. i = buffer[3];
  350. PDEBUG (0, "STV(i): Camera has %i pictures.", i);
  351. /* get current mode */
  352. if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
  353. goto error;
  354. stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
  355. /* This will attemp CIF mode, if supported. If not, set to QVGA */
  356. memset (buffer, 0, 8);
  357. if (stv680->CIF)
  358. buffer[0] = 0x00;
  359. else if (stv680->QVGA)
  360. buffer[0] = 0x03;
  361. if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
  362. PDEBUG (0, "STV(i): Set_Camera_Mode failed");
  363. i = -1;
  364. goto error;
  365. }
  366. buffer[0] = 0xf0;
  367. stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
  368. if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
  369. PDEBUG (0, "STV(e): Error setting camera video mode!");
  370. i = -1;
  371. goto error;
  372. } else {
  373. if (buffer[0] == 0) {
  374. stv680->VideoMode = 0x0000;
  375. PDEBUG (0, "STV(i): Video Mode set to CIF");
  376. }
  377. if (buffer[0] == 0x03) {
  378. stv680->VideoMode = 0x0300;
  379. PDEBUG (0, "STV(i): Video Mode set to QVGA");
  380. }
  381. }
  382. if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
  383. goto error;
  384. bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
  385. stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
  386. stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
  387. stv680->origGain = buffer[12];
  388. goto exit;
  389. error:
  390. i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
  391. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
  392. kfree(buffer);
  393. return -1;
  394. exit:
  395. kfree(buffer);
  396. /* video = 320x240, 352x288 */
  397. if (stv680->CIF == 1) {
  398. stv680->maxwidth = 352;
  399. stv680->maxheight = 288;
  400. stv680->vwidth = 352;
  401. stv680->vheight = 288;
  402. }
  403. if (stv680->QVGA == 1) {
  404. stv680->maxwidth = 320;
  405. stv680->maxheight = 240;
  406. stv680->vwidth = 320;
  407. stv680->vheight = 240;
  408. }
  409. stv680->rawbufsize = bufsize; /* must be ./. by 8 */
  410. stv680->maxframesize = bufsize * 3; /* RGB size */
  411. PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
  412. PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
  413. /* some default values */
  414. stv680->bulk_in_endpointAddr = 0x82;
  415. stv680->dropped = 0;
  416. stv680->error = 0;
  417. stv680->framecount = 0;
  418. stv680->readcount = 0;
  419. stv680->streaming = 0;
  420. /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
  421. stv680->brightness = 32767;
  422. stv680->chgbright = 0;
  423. stv680->whiteness = 0; /* only for greyscale */
  424. stv680->colour = 32767;
  425. stv680->contrast = 32767;
  426. stv680->hue = 32767;
  427. stv680->palette = STV_VIDEO_PALETTE;
  428. stv680->depth = 24; /* rgb24 bits */
  429. if ((swapRGB_on == 0) && (swapRGB == 0))
  430. PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
  431. else if ((swapRGB_on == 0) && (swapRGB == 1))
  432. PDEBUG (1, "STV(i): swapRGB is (auto) ON");
  433. else if (swapRGB_on == 1)
  434. PDEBUG (1, "STV(i): swapRGB is (forced) ON");
  435. else if (swapRGB_on == -1)
  436. PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
  437. if (stv_set_video_mode (stv680) < 0) {
  438. PDEBUG (0, "STV(e): Could not set video mode in stv_init");
  439. return -1;
  440. }
  441. return 0;
  442. }
  443. /***************** last of pencam routines *******************/
  444. /****************************************************************************
  445. * sysfs
  446. ***************************************************************************/
  447. #define stv680_file(name, variable, field) \
  448. static ssize_t show_##name(struct device *class_dev, \
  449. struct device_attribute *attr, char *buf) \
  450. { \
  451. struct video_device *vdev = to_video_device(class_dev); \
  452. struct usb_stv *stv = video_get_drvdata(vdev); \
  453. return sprintf(buf, field, stv->variable); \
  454. } \
  455. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  456. stv680_file(model, camera_name, "%s\n");
  457. stv680_file(in_use, user, "%d\n");
  458. stv680_file(streaming, streaming, "%d\n");
  459. stv680_file(palette, palette, "%i\n");
  460. stv680_file(frames_total, readcount, "%d\n");
  461. stv680_file(frames_read, framecount, "%d\n");
  462. stv680_file(packets_dropped, dropped, "%d\n");
  463. stv680_file(decoding_errors, error, "%d\n");
  464. static int stv680_create_sysfs_files(struct video_device *vdev)
  465. {
  466. int rc;
  467. rc = video_device_create_file(vdev, &dev_attr_model);
  468. if (rc) goto err;
  469. rc = video_device_create_file(vdev, &dev_attr_in_use);
  470. if (rc) goto err_model;
  471. rc = video_device_create_file(vdev, &dev_attr_streaming);
  472. if (rc) goto err_inuse;
  473. rc = video_device_create_file(vdev, &dev_attr_palette);
  474. if (rc) goto err_stream;
  475. rc = video_device_create_file(vdev, &dev_attr_frames_total);
  476. if (rc) goto err_pal;
  477. rc = video_device_create_file(vdev, &dev_attr_frames_read);
  478. if (rc) goto err_framtot;
  479. rc = video_device_create_file(vdev, &dev_attr_packets_dropped);
  480. if (rc) goto err_framread;
  481. rc = video_device_create_file(vdev, &dev_attr_decoding_errors);
  482. if (rc) goto err_dropped;
  483. return 0;
  484. err_dropped:
  485. video_device_remove_file(vdev, &dev_attr_packets_dropped);
  486. err_framread:
  487. video_device_remove_file(vdev, &dev_attr_frames_read);
  488. err_framtot:
  489. video_device_remove_file(vdev, &dev_attr_frames_total);
  490. err_pal:
  491. video_device_remove_file(vdev, &dev_attr_palette);
  492. err_stream:
  493. video_device_remove_file(vdev, &dev_attr_streaming);
  494. err_inuse:
  495. video_device_remove_file(vdev, &dev_attr_in_use);
  496. err_model:
  497. video_device_remove_file(vdev, &dev_attr_model);
  498. err:
  499. return rc;
  500. }
  501. static void stv680_remove_sysfs_files(struct video_device *vdev)
  502. {
  503. video_device_remove_file(vdev, &dev_attr_model);
  504. video_device_remove_file(vdev, &dev_attr_in_use);
  505. video_device_remove_file(vdev, &dev_attr_streaming);
  506. video_device_remove_file(vdev, &dev_attr_palette);
  507. video_device_remove_file(vdev, &dev_attr_frames_total);
  508. video_device_remove_file(vdev, &dev_attr_frames_read);
  509. video_device_remove_file(vdev, &dev_attr_packets_dropped);
  510. video_device_remove_file(vdev, &dev_attr_decoding_errors);
  511. }
  512. /********************************************************************
  513. * Camera control
  514. *******************************************************************/
  515. static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
  516. {
  517. /* This sets values for v4l interface. max/min = 65535/0 */
  518. p->brightness = stv680->brightness;
  519. p->whiteness = stv680->whiteness; /* greyscale */
  520. p->colour = stv680->colour;
  521. p->contrast = stv680->contrast;
  522. p->hue = stv680->hue;
  523. p->palette = stv680->palette;
  524. p->depth = stv680->depth;
  525. return 0;
  526. }
  527. static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
  528. {
  529. /* See above stv680_get_pict */
  530. if (p->palette != STV_VIDEO_PALETTE) {
  531. PDEBUG (2, "STV(e): Palette set error in _set_pic");
  532. return 1;
  533. }
  534. if (stv680->brightness != p->brightness) {
  535. stv680->chgbright = 1;
  536. stv680->brightness = p->brightness;
  537. }
  538. stv680->whiteness = p->whiteness; /* greyscale */
  539. stv680->colour = p->colour;
  540. stv680->contrast = p->contrast;
  541. stv680->hue = p->hue;
  542. stv680->palette = p->palette;
  543. stv680->depth = p->depth;
  544. return 0;
  545. }
  546. static void stv680_video_irq (struct urb *urb)
  547. {
  548. struct usb_stv *stv680 = urb->context;
  549. int length = urb->actual_length;
  550. if (length < stv680->rawbufsize)
  551. PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
  552. /* ohoh... */
  553. if (!stv680->streaming)
  554. return;
  555. if (!stv680->udev) {
  556. PDEBUG (0, "STV(e): device vapourished in video_irq");
  557. return;
  558. }
  559. /* 0 sized packets happen if we are to fast, but sometimes the camera
  560. keeps sending them forever...
  561. */
  562. if (length && !urb->status) {
  563. stv680->nullpackets = 0;
  564. switch (stv680->scratch[stv680->scratch_next].state) {
  565. case BUFFER_READY:
  566. case BUFFER_BUSY:
  567. stv680->dropped++;
  568. break;
  569. case BUFFER_UNUSED:
  570. memcpy (stv680->scratch[stv680->scratch_next].data,
  571. (unsigned char *) urb->transfer_buffer, length);
  572. stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
  573. stv680->scratch[stv680->scratch_next].length = length;
  574. if (waitqueue_active (&stv680->wq)) {
  575. wake_up_interruptible (&stv680->wq);
  576. }
  577. stv680->scratch_overflow = 0;
  578. stv680->scratch_next++;
  579. if (stv680->scratch_next >= STV680_NUMSCRATCH)
  580. stv680->scratch_next = 0;
  581. break;
  582. } /* switch */
  583. } else {
  584. stv680->nullpackets++;
  585. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  586. if (waitqueue_active (&stv680->wq)) {
  587. wake_up_interruptible (&stv680->wq);
  588. }
  589. }
  590. } /* if - else */
  591. /* Resubmit urb for new data */
  592. urb->status = 0;
  593. urb->dev = stv680->udev;
  594. if (usb_submit_urb (urb, GFP_ATOMIC))
  595. PDEBUG (0, "STV(e): urb burned down in video irq");
  596. return;
  597. } /* _video_irq */
  598. static int stv680_start_stream (struct usb_stv *stv680)
  599. {
  600. struct urb *urb;
  601. int err = 0, i;
  602. stv680->streaming = 1;
  603. /* Do some memory allocation */
  604. for (i = 0; i < STV680_NUMFRAMES; i++) {
  605. stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
  606. stv680->frame[i].curpix = 0;
  607. }
  608. /* packet size = 4096 */
  609. for (i = 0; i < STV680_NUMSBUF; i++) {
  610. stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  611. if (stv680->sbuf[i].data == NULL) {
  612. PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
  613. goto nomem_err;
  614. }
  615. }
  616. stv680->scratch_next = 0;
  617. stv680->scratch_use = 0;
  618. stv680->scratch_overflow = 0;
  619. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  620. stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  621. if (stv680->scratch[i].data == NULL) {
  622. PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
  623. goto nomem_err;
  624. }
  625. stv680->scratch[i].state = BUFFER_UNUSED;
  626. }
  627. for (i = 0; i < STV680_NUMSBUF; i++) {
  628. urb = usb_alloc_urb (0, GFP_KERNEL);
  629. if (!urb)
  630. goto nomem_err;
  631. /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
  632. usb_fill_bulk_urb (urb, stv680->udev,
  633. usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
  634. stv680->sbuf[i].data, stv680->rawbufsize,
  635. stv680_video_irq, stv680);
  636. stv680->urb[i] = urb;
  637. err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
  638. if (err) {
  639. PDEBUG (0, "STV(e): urb burned down with err "
  640. "%d in start stream %d", err, i);
  641. goto nomem_err;
  642. }
  643. } /* i STV680_NUMSBUF */
  644. stv680->framecount = 0;
  645. return 0;
  646. nomem_err:
  647. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  648. kfree(stv680->scratch[i].data);
  649. stv680->scratch[i].data = NULL;
  650. }
  651. for (i = 0; i < STV680_NUMSBUF; i++) {
  652. usb_kill_urb(stv680->urb[i]);
  653. usb_free_urb(stv680->urb[i]);
  654. stv680->urb[i] = NULL;
  655. kfree(stv680->sbuf[i].data);
  656. stv680->sbuf[i].data = NULL;
  657. }
  658. return -ENOMEM;
  659. }
  660. static int stv680_stop_stream (struct usb_stv *stv680)
  661. {
  662. int i;
  663. if (!stv680->streaming || !stv680->udev)
  664. return 1;
  665. stv680->streaming = 0;
  666. for (i = 0; i < STV680_NUMSBUF; i++)
  667. if (stv680->urb[i]) {
  668. usb_kill_urb (stv680->urb[i]);
  669. usb_free_urb (stv680->urb[i]);
  670. stv680->urb[i] = NULL;
  671. kfree(stv680->sbuf[i].data);
  672. }
  673. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  674. kfree(stv680->scratch[i].data);
  675. stv680->scratch[i].data = NULL;
  676. }
  677. return 0;
  678. }
  679. static int stv680_set_size (struct usb_stv *stv680, int width, int height)
  680. {
  681. int wasstreaming = stv680->streaming;
  682. /* Check to see if we need to change */
  683. if ((stv680->vwidth == width) && (stv680->vheight == height))
  684. return 0;
  685. PDEBUG (1, "STV(i): size request for %i x %i", width, height);
  686. /* Check for a valid mode */
  687. if ((!width || !height) || ((width & 1) || (height & 1))) {
  688. PDEBUG (1, "STV(e): set_size error: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  689. return 1;
  690. }
  691. if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
  692. width = stv680->maxwidth / 2;
  693. height = stv680->maxheight / 2;
  694. } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
  695. width = 160;
  696. height = 120;
  697. } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
  698. width = 176;
  699. height = 144;
  700. } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
  701. width = 320;
  702. height = 240;
  703. } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
  704. width = 352;
  705. height = 288;
  706. } else {
  707. PDEBUG (1, "STV(e): request for non-supported size: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  708. return 1;
  709. }
  710. /* Stop a current stream and start it again at the new size */
  711. if (wasstreaming)
  712. stv680_stop_stream (stv680);
  713. stv680->vwidth = width;
  714. stv680->vheight = height;
  715. PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
  716. if (wasstreaming)
  717. stv680_start_stream (stv680);
  718. return 0;
  719. }
  720. /**********************************************************************
  721. * Video Decoding
  722. **********************************************************************/
  723. /******* routines from the pencam program; hey, they work! ********/
  724. /*
  725. * STV0680 Vision Camera Chipset Driver
  726. * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
  727. */
  728. #define RED 0
  729. #define GREEN 1
  730. #define BLUE 2
  731. #define AD(x, y, w) (((y)*(w)+(x))*3)
  732. static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
  733. {
  734. int x, y, i;
  735. int w = stv680->cwidth;
  736. int vw = stv680->cwidth, vh = stv680->cheight;
  737. unsigned int p = 0;
  738. int colour = 0, bayer = 0;
  739. unsigned char *raw = buffer->data;
  740. struct stv680_frame *frame = &stv680->frame[stv680->curframe];
  741. unsigned char *output = frame->data;
  742. unsigned char *temp = frame->data;
  743. int offset = buffer->offset;
  744. if (frame->curpix == 0) {
  745. if (frame->grabstate == FRAME_READY) {
  746. frame->grabstate = FRAME_GRABBING;
  747. }
  748. }
  749. if (offset != frame->curpix) { /* Regard frame as lost :( */
  750. frame->curpix = 0;
  751. stv680->error++;
  752. return;
  753. }
  754. if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
  755. vw = 320;
  756. vh = 240;
  757. }
  758. if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
  759. vw = 352;
  760. vh = 288;
  761. }
  762. memset (output, 0, 3 * vw * vh); /* clear output matrix. */
  763. for (y = 0; y < vh; y++) {
  764. for (x = 0; x < vw; x++) {
  765. if (x & 1)
  766. p = *(raw + y * w + (x >> 1));
  767. else
  768. p = *(raw + y * w + (x >> 1) + (w >> 1));
  769. if (y & 1)
  770. bayer = 2;
  771. else
  772. bayer = 0;
  773. if (x & 1)
  774. bayer++;
  775. switch (bayer) {
  776. case 0:
  777. case 3:
  778. colour = 1;
  779. break;
  780. case 1:
  781. colour = 0;
  782. break;
  783. case 2:
  784. colour = 2;
  785. break;
  786. }
  787. i = (y * vw + x) * 3;
  788. *(output + i + colour) = (unsigned char) p;
  789. } /* for x */
  790. } /* for y */
  791. /****** gamma correction plus hardcoded white balance */
  792. /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
  793. Correction values red[], green[], blue[], are generated by
  794. (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
  795. White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
  796. converted to unsigned char. Values are in stv680.h */
  797. for (y = 0; y < vh; y++) {
  798. for (x = 0; x < vw; x++) {
  799. i = (y * vw + x) * 3;
  800. *(output + i) = red[*(output + i)];
  801. *(output + i + 1) = green[*(output + i + 1)];
  802. *(output + i + 2) = blue[*(output + i + 2)];
  803. }
  804. }
  805. /****** bayer demosaic ******/
  806. for (y = 1; y < (vh - 1); y++) {
  807. for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
  808. if (y & 1)
  809. bayer = 0;
  810. else
  811. bayer = 2;
  812. if (!(x & 1))
  813. bayer++;
  814. switch (bayer) {
  815. case 0: /* green. blue lr, red tb */
  816. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
  817. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
  818. break;
  819. case 1: /* blue. green lrtb, red diagonals */
  820. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  821. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
  822. break;
  823. case 2: /* red. green lrtb, blue diagonals */
  824. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  825. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
  826. break;
  827. case 3: /* green. red lr, blue tb */
  828. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
  829. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
  830. break;
  831. } /* switch */
  832. } /* for x */
  833. } /* for y - end demosaic */
  834. /* fix top and bottom row, left and right side */
  835. i = vw * 3;
  836. memcpy (output, (output + i), i);
  837. memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
  838. for (y = 0; y < vh; y++) {
  839. i = y * vw * 3;
  840. memcpy ((output + i), (output + i + 3), 3);
  841. memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
  842. }
  843. /* process all raw data, then trim to size if necessary */
  844. if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
  845. i = 0;
  846. for (y = 0; y < vh; y++) {
  847. if (!(y & 1)) {
  848. for (x = 0; x < vw; x++) {
  849. p = (y * vw + x) * 3;
  850. if (!(x & 1)) {
  851. *(output + i) = *(output + p);
  852. *(output + i + 1) = *(output + p + 1);
  853. *(output + i + 2) = *(output + p + 2);
  854. i += 3;
  855. }
  856. } /* for x */
  857. }
  858. } /* for y */
  859. }
  860. /* reset to proper width */
  861. if ((stv680->vwidth == 160)) {
  862. vw = 160;
  863. vh = 120;
  864. }
  865. if ((stv680->vwidth == 176)) {
  866. vw = 176;
  867. vh = 144;
  868. }
  869. /* output is RGB; some programs want BGR */
  870. /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
  871. /* swapRGB_on=-1, never swap */
  872. if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
  873. for (y = 0; y < vh; y++) {
  874. for (x = 0; x < vw; x++) {
  875. i = (y * vw + x) * 3;
  876. *(temp) = *(output + i);
  877. *(output + i) = *(output + i + 2);
  878. *(output + i + 2) = *(temp);
  879. }
  880. }
  881. }
  882. /* brightness */
  883. if (stv680->chgbright == 1) {
  884. if (stv680->brightness >= 32767) {
  885. p = (stv680->brightness - 32767) / 256;
  886. for (x = 0; x < (vw * vh * 3); x++) {
  887. if ((*(output + x) + (unsigned char) p) > 255)
  888. *(output + x) = 255;
  889. else
  890. *(output + x) += (unsigned char) p;
  891. } /* for */
  892. } else {
  893. p = (32767 - stv680->brightness) / 256;
  894. for (x = 0; x < (vw * vh * 3); x++) {
  895. if ((unsigned char) p > *(output + x))
  896. *(output + x) = 0;
  897. else
  898. *(output + x) -= (unsigned char) p;
  899. } /* for */
  900. } /* else */
  901. }
  902. /* if */
  903. frame->curpix = 0;
  904. frame->curlinepix = 0;
  905. frame->grabstate = FRAME_DONE;
  906. stv680->framecount++;
  907. stv680->readcount++;
  908. if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
  909. stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
  910. }
  911. } /* bayer_unshuffle */
  912. /******* end routines from the pencam program *********/
  913. static int stv680_newframe (struct usb_stv *stv680, int framenr)
  914. {
  915. int errors = 0;
  916. while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
  917. if (!stv680->frame[framenr].curpix) {
  918. errors++;
  919. }
  920. wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
  921. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  922. stv680->nullpackets = 0;
  923. PDEBUG (2, "STV(i): too many null length packets, restarting capture");
  924. stv680_stop_stream (stv680);
  925. stv680_start_stream (stv680);
  926. } else {
  927. if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
  928. stv680->frame[framenr].grabstate = FRAME_ERROR;
  929. PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
  930. return -EIO;
  931. }
  932. stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
  933. bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
  934. stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
  935. stv680->scratch_use++;
  936. if (stv680->scratch_use >= STV680_NUMSCRATCH)
  937. stv680->scratch_use = 0;
  938. if (errors > STV680_MAX_ERRORS) {
  939. errors = 0;
  940. PDEBUG (2, "STV(i): too many errors, restarting capture");
  941. stv680_stop_stream (stv680);
  942. stv680_start_stream (stv680);
  943. }
  944. } /* else */
  945. } /* while */
  946. return 0;
  947. }
  948. /*********************************************************************
  949. * Video4Linux
  950. *********************************************************************/
  951. static int stv_open (struct inode *inode, struct file *file)
  952. {
  953. struct video_device *dev = video_devdata(file);
  954. struct usb_stv *stv680 = video_get_drvdata(dev);
  955. int err = 0;
  956. /* we are called with the BKL held */
  957. stv680->user = 1;
  958. err = stv_init (stv680); /* main initialization routine for camera */
  959. if (err >= 0) {
  960. stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
  961. if (!stv680->fbuf) {
  962. PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
  963. err = -ENOMEM;
  964. }
  965. file->private_data = dev;
  966. }
  967. if (err)
  968. stv680->user = 0;
  969. return err;
  970. }
  971. static int stv_close (struct inode *inode, struct file *file)
  972. {
  973. struct video_device *dev = file->private_data;
  974. struct usb_stv *stv680 = video_get_drvdata(dev);
  975. int i;
  976. for (i = 0; i < STV680_NUMFRAMES; i++)
  977. stv680->frame[i].grabstate = FRAME_UNUSED;
  978. if (stv680->streaming)
  979. stv680_stop_stream (stv680);
  980. if ((i = stv_stop_video (stv680)) < 0)
  981. PDEBUG (1, "STV(e): stop_video failed in stv_close");
  982. rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
  983. stv680->user = 0;
  984. if (stv680->removed) {
  985. kfree(stv680);
  986. stv680 = NULL;
  987. PDEBUG (0, "STV(i): device unregistered");
  988. }
  989. file->private_data = NULL;
  990. return 0;
  991. }
  992. static int stv680_do_ioctl (struct inode *inode, struct file *file,
  993. unsigned int cmd, void *arg)
  994. {
  995. struct video_device *vdev = file->private_data;
  996. struct usb_stv *stv680 = video_get_drvdata(vdev);
  997. if (!stv680->udev)
  998. return -EIO;
  999. switch (cmd) {
  1000. case VIDIOCGCAP:{
  1001. struct video_capability *b = arg;
  1002. strcpy (b->name, stv680->camera_name);
  1003. b->type = VID_TYPE_CAPTURE;
  1004. b->channels = 1;
  1005. b->audios = 0;
  1006. b->maxwidth = stv680->maxwidth;
  1007. b->maxheight = stv680->maxheight;
  1008. b->minwidth = stv680->maxwidth / 2;
  1009. b->minheight = stv680->maxheight / 2;
  1010. return 0;
  1011. }
  1012. case VIDIOCGCHAN:{
  1013. struct video_channel *v = arg;
  1014. if (v->channel != 0)
  1015. return -EINVAL;
  1016. v->flags = 0;
  1017. v->tuners = 0;
  1018. v->type = VIDEO_TYPE_CAMERA;
  1019. strcpy (v->name, "STV Camera");
  1020. return 0;
  1021. }
  1022. case VIDIOCSCHAN:{
  1023. struct video_channel *v = arg;
  1024. if (v->channel != 0)
  1025. return -EINVAL;
  1026. return 0;
  1027. }
  1028. case VIDIOCGPICT:{
  1029. struct video_picture *p = arg;
  1030. stv680_get_pict (stv680, p);
  1031. return 0;
  1032. }
  1033. case VIDIOCSPICT:{
  1034. struct video_picture *p = arg;
  1035. if (stv680_set_pict (stv680, p))
  1036. return -EINVAL;
  1037. return 0;
  1038. }
  1039. case VIDIOCSWIN:{
  1040. struct video_window *vw = arg;
  1041. if (vw->flags)
  1042. return -EINVAL;
  1043. if (vw->clipcount)
  1044. return -EINVAL;
  1045. if (vw->width != stv680->vwidth) {
  1046. if (stv680_set_size (stv680, vw->width, vw->height)) {
  1047. PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. return 0;
  1052. }
  1053. case VIDIOCGWIN:{
  1054. struct video_window *vw = arg;
  1055. vw->x = 0; /* FIXME */
  1056. vw->y = 0;
  1057. vw->chromakey = 0;
  1058. vw->flags = 0;
  1059. vw->clipcount = 0;
  1060. vw->width = stv680->vwidth;
  1061. vw->height = stv680->vheight;
  1062. return 0;
  1063. }
  1064. case VIDIOCGMBUF:{
  1065. struct video_mbuf *vm = arg;
  1066. int i;
  1067. memset (vm, 0, sizeof (*vm));
  1068. vm->size = STV680_NUMFRAMES * stv680->maxframesize;
  1069. vm->frames = STV680_NUMFRAMES;
  1070. for (i = 0; i < STV680_NUMFRAMES; i++)
  1071. vm->offsets[i] = stv680->maxframesize * i;
  1072. return 0;
  1073. }
  1074. case VIDIOCMCAPTURE:{
  1075. struct video_mmap *vm = arg;
  1076. if (vm->format != STV_VIDEO_PALETTE) {
  1077. PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
  1078. vm->format, STV_VIDEO_PALETTE);
  1079. if ((vm->format == 3) && (swapRGB_on == 0)) {
  1080. PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
  1081. /* this may fix those apps (e.g., xawtv) that want BGR */
  1082. swapRGB = 1;
  1083. }
  1084. return -EINVAL;
  1085. }
  1086. if (vm->frame >= STV680_NUMFRAMES) {
  1087. PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
  1088. return -EINVAL;
  1089. }
  1090. if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
  1091. || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
  1092. PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
  1093. stv680->frame[vm->frame].grabstate);
  1094. return -EBUSY;
  1095. }
  1096. /* Is this according to the v4l spec??? */
  1097. if (stv680->vwidth != vm->width) {
  1098. if (stv680_set_size (stv680, vm->width, vm->height)) {
  1099. PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
  1100. return -EINVAL;
  1101. }
  1102. }
  1103. stv680->frame[vm->frame].grabstate = FRAME_READY;
  1104. if (!stv680->streaming)
  1105. stv680_start_stream (stv680);
  1106. return 0;
  1107. }
  1108. case VIDIOCSYNC:{
  1109. int *frame = arg;
  1110. int ret = 0;
  1111. if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
  1112. PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
  1113. return -EINVAL;
  1114. }
  1115. ret = stv680_newframe (stv680, *frame);
  1116. stv680->frame[*frame].grabstate = FRAME_UNUSED;
  1117. return ret;
  1118. }
  1119. case VIDIOCGFBUF:{
  1120. struct video_buffer *vb = arg;
  1121. memset (vb, 0, sizeof (*vb));
  1122. return 0;
  1123. }
  1124. case VIDIOCKEY:
  1125. return 0;
  1126. case VIDIOCCAPTURE:
  1127. {
  1128. PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
  1129. return -EINVAL;
  1130. }
  1131. case VIDIOCSFBUF:
  1132. case VIDIOCGTUNER:
  1133. case VIDIOCSTUNER:
  1134. case VIDIOCGFREQ:
  1135. case VIDIOCSFREQ:
  1136. case VIDIOCGAUDIO:
  1137. case VIDIOCSAUDIO:
  1138. return -EINVAL;
  1139. default:
  1140. return -ENOIOCTLCMD;
  1141. } /* end switch */
  1142. return 0;
  1143. }
  1144. static int stv680_ioctl(struct inode *inode, struct file *file,
  1145. unsigned int cmd, unsigned long arg)
  1146. {
  1147. return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
  1148. }
  1149. static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
  1150. {
  1151. struct video_device *dev = file->private_data;
  1152. struct usb_stv *stv680 = video_get_drvdata(dev);
  1153. unsigned long start = vma->vm_start;
  1154. unsigned long size = vma->vm_end-vma->vm_start;
  1155. unsigned long page, pos;
  1156. mutex_lock(&stv680->lock);
  1157. if (stv680->udev == NULL) {
  1158. mutex_unlock(&stv680->lock);
  1159. return -EIO;
  1160. }
  1161. if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
  1162. & ~(PAGE_SIZE - 1))) {
  1163. mutex_unlock(&stv680->lock);
  1164. return -EINVAL;
  1165. }
  1166. pos = (unsigned long) stv680->fbuf;
  1167. while (size > 0) {
  1168. page = vmalloc_to_pfn((void *)pos);
  1169. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1170. mutex_unlock(&stv680->lock);
  1171. return -EAGAIN;
  1172. }
  1173. start += PAGE_SIZE;
  1174. pos += PAGE_SIZE;
  1175. if (size > PAGE_SIZE)
  1176. size -= PAGE_SIZE;
  1177. else
  1178. size = 0;
  1179. }
  1180. mutex_unlock(&stv680->lock);
  1181. return 0;
  1182. }
  1183. static ssize_t stv680_read (struct file *file, char __user *buf,
  1184. size_t count, loff_t *ppos)
  1185. {
  1186. struct video_device *dev = file->private_data;
  1187. unsigned long int realcount = count;
  1188. int ret = 0;
  1189. struct usb_stv *stv680 = video_get_drvdata(dev);
  1190. unsigned long int i;
  1191. if (STV680_NUMFRAMES != 2) {
  1192. PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
  1193. return -1;
  1194. }
  1195. if (stv680->udev == NULL)
  1196. return -EIO;
  1197. if (realcount > (stv680->vwidth * stv680->vheight * 3))
  1198. realcount = stv680->vwidth * stv680->vheight * 3;
  1199. /* Shouldn't happen: */
  1200. if (stv680->frame[0].grabstate == FRAME_GRABBING) {
  1201. PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
  1202. return -EBUSY;
  1203. }
  1204. stv680->frame[0].grabstate = FRAME_READY;
  1205. stv680->frame[1].grabstate = FRAME_UNUSED;
  1206. stv680->curframe = 0;
  1207. if (!stv680->streaming)
  1208. stv680_start_stream (stv680);
  1209. if (!stv680->streaming) {
  1210. ret = stv680_newframe (stv680, 0); /* ret should = 0 */
  1211. }
  1212. ret = stv680_newframe (stv680, 0);
  1213. if (!ret) {
  1214. if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
  1215. PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
  1216. return -EFAULT;
  1217. }
  1218. } else {
  1219. realcount = ret;
  1220. }
  1221. stv680->frame[0].grabstate = FRAME_UNUSED;
  1222. return realcount;
  1223. } /* stv680_read */
  1224. static const struct file_operations stv680_fops = {
  1225. .owner = THIS_MODULE,
  1226. .open = stv_open,
  1227. .release = stv_close,
  1228. .read = stv680_read,
  1229. .mmap = stv680_mmap,
  1230. .ioctl = stv680_ioctl,
  1231. #ifdef CONFIG_COMPAT
  1232. .compat_ioctl = v4l_compat_ioctl32,
  1233. #endif
  1234. .llseek = no_llseek,
  1235. };
  1236. static struct video_device stv680_template = {
  1237. .name = "STV0680 USB camera",
  1238. .type = VID_TYPE_CAPTURE,
  1239. .fops = &stv680_fops,
  1240. .release = video_device_release,
  1241. .minor = -1,
  1242. };
  1243. static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
  1244. {
  1245. struct usb_device *dev = interface_to_usbdev(intf);
  1246. struct usb_host_interface *interface;
  1247. struct usb_stv *stv680 = NULL;
  1248. char *camera_name = NULL;
  1249. int retval = 0;
  1250. /* We don't handle multi-config cameras */
  1251. if (dev->descriptor.bNumConfigurations != 1) {
  1252. PDEBUG (0, "STV(e): Number of Configurations != 1");
  1253. return -ENODEV;
  1254. }
  1255. interface = &intf->altsetting[0];
  1256. /* Is it a STV680? */
  1257. if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
  1258. (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
  1259. camera_name = "STV0680";
  1260. PDEBUG (0, "STV(i): STV0680 camera found.");
  1261. } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
  1262. (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
  1263. camera_name = "Creative WebCam Go Mini";
  1264. PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
  1265. } else {
  1266. PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
  1267. PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
  1268. retval = -ENODEV;
  1269. goto error;
  1270. }
  1271. /* We found one */
  1272. if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
  1273. PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
  1274. retval = -ENOMEM;
  1275. goto error;
  1276. }
  1277. stv680->udev = dev;
  1278. stv680->camera_name = camera_name;
  1279. stv680->vdev = video_device_alloc();
  1280. if (!stv680->vdev) {
  1281. retval = -ENOMEM;
  1282. goto error;
  1283. }
  1284. memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
  1285. stv680->vdev->parent = &intf->dev;
  1286. video_set_drvdata(stv680->vdev, stv680);
  1287. memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
  1288. init_waitqueue_head (&stv680->wq);
  1289. mutex_init (&stv680->lock);
  1290. wmb ();
  1291. if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
  1292. PDEBUG (0, "STV(e): video_register_device failed");
  1293. retval = -EIO;
  1294. goto error_vdev;
  1295. }
  1296. PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
  1297. usb_set_intfdata (intf, stv680);
  1298. retval = stv680_create_sysfs_files(stv680->vdev);
  1299. if (retval)
  1300. goto error_unreg;
  1301. return 0;
  1302. error_unreg:
  1303. video_unregister_device(stv680->vdev);
  1304. error_vdev:
  1305. video_device_release(stv680->vdev);
  1306. error:
  1307. kfree(stv680);
  1308. return retval;
  1309. }
  1310. static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
  1311. {
  1312. int i;
  1313. stv680->udev = NULL;
  1314. stv680->frame[0].grabstate = FRAME_ERROR;
  1315. stv680->frame[1].grabstate = FRAME_ERROR;
  1316. stv680->streaming = 0;
  1317. wake_up_interruptible (&stv680->wq);
  1318. for (i = 0; i < STV680_NUMSBUF; i++)
  1319. if (stv680->urb[i]) {
  1320. usb_kill_urb (stv680->urb[i]);
  1321. usb_free_urb (stv680->urb[i]);
  1322. stv680->urb[i] = NULL;
  1323. kfree(stv680->sbuf[i].data);
  1324. }
  1325. for (i = 0; i < STV680_NUMSCRATCH; i++)
  1326. kfree(stv680->scratch[i].data);
  1327. PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
  1328. /* Free the memory */
  1329. kfree(stv680);
  1330. }
  1331. static void stv680_disconnect (struct usb_interface *intf)
  1332. {
  1333. struct usb_stv *stv680 = usb_get_intfdata (intf);
  1334. usb_set_intfdata (intf, NULL);
  1335. if (stv680) {
  1336. /* We don't want people trying to open up the device */
  1337. if (stv680->vdev) {
  1338. stv680_remove_sysfs_files(stv680->vdev);
  1339. video_unregister_device(stv680->vdev);
  1340. stv680->vdev = NULL;
  1341. }
  1342. if (!stv680->user) {
  1343. usb_stv680_remove_disconnected (stv680);
  1344. } else {
  1345. stv680->removed = 1;
  1346. }
  1347. }
  1348. }
  1349. static struct usb_driver stv680_driver = {
  1350. .name = "stv680",
  1351. .probe = stv680_probe,
  1352. .disconnect = stv680_disconnect,
  1353. .id_table = device_table
  1354. };
  1355. /********************************************************************
  1356. * Module routines
  1357. ********************************************************************/
  1358. static int __init usb_stv680_init (void)
  1359. {
  1360. if (usb_register (&stv680_driver) < 0) {
  1361. PDEBUG (0, "STV(e): Could not setup STV0680 driver");
  1362. return -1;
  1363. }
  1364. PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
  1365. info(DRIVER_DESC " " DRIVER_VERSION);
  1366. return 0;
  1367. }
  1368. static void __exit usb_stv680_exit (void)
  1369. {
  1370. usb_deregister (&stv680_driver);
  1371. PDEBUG (0, "STV(i): driver deregistered");
  1372. }
  1373. module_init (usb_stv680_init);
  1374. module_exit (usb_stv680_exit);