stv680.c 43 KB

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