stv680.c 44 KB

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