stv680.c 43 KB

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