stk-webcam.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
  3. *
  4. * Copyright (C) 2006 Nicolas VIVIEN
  5. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  6. *
  7. * Some parts are inspired from cafe_ccic.c
  8. * Copyright 2006-2007 Jonathan Corbet
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/kref.h>
  30. #include <linux/usb.h>
  31. #include <linux/mm.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/videodev2.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include "stk-webcam.h"
  37. static int hflip = 1;
  38. module_param(hflip, bool, 0444);
  39. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
  40. static int vflip = 1;
  41. module_param(vflip, bool, 0444);
  42. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
  43. static int debug;
  44. module_param(debug, int, 0444);
  45. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  46. MODULE_LICENSE("GPL");
  47. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  48. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  49. /* Some cameras have audio interfaces, we aren't interested in those */
  50. static struct usb_device_id stkwebcam_table[] = {
  51. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  52. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  53. { }
  54. };
  55. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  56. static void stk_camera_cleanup(struct kref *kref)
  57. {
  58. struct stk_camera *dev = to_stk_camera(kref);
  59. STK_INFO("Syntek USB2.0 Camera release resources"
  60. " video device /dev/video%d\n", dev->vdev.minor);
  61. video_unregister_device(&dev->vdev);
  62. dev->vdev.priv = NULL;
  63. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  64. STK_ERROR("We are leaking memory\n");
  65. usb_put_intf(dev->interface);
  66. kfree(dev);
  67. }
  68. /*
  69. * Basic stuff
  70. */
  71. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  72. {
  73. struct usb_device *udev = dev->udev;
  74. int ret;
  75. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  76. 0x01,
  77. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  78. value,
  79. index,
  80. NULL,
  81. 0,
  82. 500);
  83. if (ret < 0)
  84. return ret;
  85. else
  86. return 0;
  87. }
  88. int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
  89. {
  90. struct usb_device *udev = dev->udev;
  91. int ret;
  92. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  93. 0x00,
  94. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  95. 0x00,
  96. index,
  97. (u8 *) value,
  98. sizeof(u8),
  99. 500);
  100. if (ret < 0)
  101. return ret;
  102. else
  103. return 0;
  104. }
  105. static int stk_start_stream(struct stk_camera *dev)
  106. {
  107. int value;
  108. int i, ret;
  109. int value_116, value_117;
  110. if (!is_present(dev))
  111. return -ENODEV;
  112. if (!is_memallocd(dev) || !is_initialised(dev)) {
  113. STK_ERROR("FIXME: Buffers are not allocated\n");
  114. return -EFAULT;
  115. }
  116. ret = usb_set_interface(dev->udev, 0, 5);
  117. if (ret < 0)
  118. STK_ERROR("usb_set_interface failed !\n");
  119. if (stk_sensor_wakeup(dev))
  120. STK_ERROR("error awaking the sensor\n");
  121. stk_camera_read_reg(dev, 0x0116, &value_116);
  122. stk_camera_read_reg(dev, 0x0117, &value_117);
  123. stk_camera_write_reg(dev, 0x0116, 0x0000);
  124. stk_camera_write_reg(dev, 0x0117, 0x0000);
  125. stk_camera_read_reg(dev, 0x0100, &value);
  126. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  127. stk_camera_write_reg(dev, 0x0116, value_116);
  128. stk_camera_write_reg(dev, 0x0117, value_117);
  129. for (i = 0; i < MAX_ISO_BUFS; i++) {
  130. if (dev->isobufs[i].urb) {
  131. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  132. atomic_inc(&dev->urbs_used);
  133. if (ret)
  134. return ret;
  135. }
  136. }
  137. set_streaming(dev);
  138. return 0;
  139. }
  140. static int stk_stop_stream(struct stk_camera *dev)
  141. {
  142. int value;
  143. int i;
  144. if (is_present(dev)) {
  145. stk_camera_read_reg(dev, 0x0100, &value);
  146. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  147. if (dev->isobufs != NULL) {
  148. for (i = 0; i < MAX_ISO_BUFS; i++) {
  149. if (dev->isobufs[i].urb)
  150. usb_kill_urb(dev->isobufs[i].urb);
  151. }
  152. }
  153. unset_streaming(dev);
  154. if (usb_set_interface(dev->udev, 0, 0))
  155. STK_ERROR("usb_set_interface failed !\n");
  156. if (stk_sensor_sleep(dev))
  157. STK_ERROR("error suspending the sensor\n");
  158. }
  159. return 0;
  160. }
  161. /*
  162. * This seems to be the shortest init sequence we
  163. * must do in order to find the sensor
  164. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  165. * is also reset. Maybe powers down it?
  166. * Rest of values don't make a difference
  167. */
  168. static struct regval stk1125_initvals[] = {
  169. /*TODO: What means this sequence? */
  170. {0x0000, 0x24},
  171. {0x0100, 0x21},
  172. {0x0002, 0x68},
  173. {0x0003, 0x80},
  174. {0x0005, 0x00},
  175. {0x0007, 0x03},
  176. {0x000d, 0x00},
  177. {0x000f, 0x02},
  178. {0x0300, 0x12},
  179. {0x0350, 0x41},
  180. {0x0351, 0x00},
  181. {0x0352, 0x00},
  182. {0x0353, 0x00},
  183. {0x0018, 0x10},
  184. {0x0019, 0x00},
  185. {0x001b, 0x0e},
  186. {0x001c, 0x46},
  187. {0x0300, 0x80},
  188. {0x001a, 0x04},
  189. {0x0110, 0x00},
  190. {0x0111, 0x00},
  191. {0x0112, 0x00},
  192. {0x0113, 0x00},
  193. {0xffff, 0xff},
  194. };
  195. static int stk_initialise(struct stk_camera *dev)
  196. {
  197. struct regval *rv;
  198. int ret;
  199. if (!is_present(dev))
  200. return -ENODEV;
  201. if (is_initialised(dev))
  202. return 0;
  203. rv = stk1125_initvals;
  204. while (rv->reg != 0xffff) {
  205. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  206. if (ret)
  207. return ret;
  208. rv++;
  209. }
  210. if (stk_sensor_init(dev) == 0) {
  211. set_initialised(dev);
  212. return 0;
  213. } else
  214. return -1;
  215. }
  216. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  217. /* sysfs functions */
  218. /*FIXME cleanup this */
  219. static ssize_t show_brightness(struct device *class,
  220. struct device_attribute *attr, char *buf)
  221. {
  222. struct video_device *vdev = to_video_device(class);
  223. struct stk_camera *dev = vdev_to_camera(vdev);
  224. return sprintf(buf, "%X\n", dev->vsettings.brightness);
  225. }
  226. static ssize_t store_brightness(struct device *class,
  227. struct device_attribute *attr, const char *buf, size_t count)
  228. {
  229. char *endp;
  230. unsigned long value;
  231. int ret;
  232. struct video_device *vdev = to_video_device(class);
  233. struct stk_camera *dev = vdev_to_camera(vdev);
  234. value = simple_strtoul(buf, &endp, 16);
  235. dev->vsettings.brightness = (int) value;
  236. ret = stk_sensor_set_brightness(dev, value >> 8);
  237. if (ret)
  238. return ret;
  239. else
  240. return count;
  241. }
  242. static ssize_t show_hflip(struct device *class,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct video_device *vdev = to_video_device(class);
  246. struct stk_camera *dev = vdev_to_camera(vdev);
  247. return sprintf(buf, "%d\n", dev->vsettings.hflip);
  248. }
  249. static ssize_t store_hflip(struct device *class,
  250. struct device_attribute *attr, const char *buf, size_t count)
  251. {
  252. struct video_device *vdev = to_video_device(class);
  253. struct stk_camera *dev = vdev_to_camera(vdev);
  254. if (strncmp(buf, "1", 1) == 0)
  255. dev->vsettings.hflip = 1;
  256. else if (strncmp(buf, "0", 1) == 0)
  257. dev->vsettings.hflip = 0;
  258. else
  259. return -EINVAL;
  260. return strlen(buf);
  261. }
  262. static ssize_t show_vflip(struct device *class,
  263. struct device_attribute *attr, char *buf)
  264. {
  265. struct video_device *vdev = to_video_device(class);
  266. struct stk_camera *dev = vdev_to_camera(vdev);
  267. return sprintf(buf, "%d\n", dev->vsettings.vflip);
  268. }
  269. static ssize_t store_vflip(struct device *class,
  270. struct device_attribute *attr, const char *buf, size_t count)
  271. {
  272. struct video_device *vdev = to_video_device(class);
  273. struct stk_camera *dev = vdev_to_camera(vdev);
  274. if (strncmp(buf, "1", 1) == 0)
  275. dev->vsettings.vflip = 1;
  276. else if (strncmp(buf, "0", 1) == 0)
  277. dev->vsettings.vflip = 0;
  278. else
  279. return -EINVAL;
  280. return strlen(buf);
  281. }
  282. static DEVICE_ATTR(brightness, S_IRUGO | S_IWUGO,
  283. show_brightness, store_brightness);
  284. static DEVICE_ATTR(hflip, S_IRUGO | S_IWUGO, show_hflip, store_hflip);
  285. static DEVICE_ATTR(vflip, S_IRUGO | S_IWUGO, show_vflip, store_vflip);
  286. static int stk_create_sysfs_files(struct video_device *vdev)
  287. {
  288. int ret;
  289. ret = device_create_file(&vdev->dev, &dev_attr_brightness);
  290. ret += device_create_file(&vdev->dev, &dev_attr_hflip);
  291. ret += device_create_file(&vdev->dev, &dev_attr_vflip);
  292. if (ret)
  293. STK_WARNING("Could not create sysfs files\n");
  294. return ret;
  295. }
  296. static void stk_remove_sysfs_files(struct video_device *vdev)
  297. {
  298. device_remove_file(&vdev->dev, &dev_attr_brightness);
  299. device_remove_file(&vdev->dev, &dev_attr_hflip);
  300. device_remove_file(&vdev->dev, &dev_attr_vflip);
  301. }
  302. #else
  303. #define stk_create_sysfs_files(a)
  304. #define stk_remove_sysfs_files(a)
  305. #endif
  306. /* *********************************************** */
  307. /*
  308. * This function is called as an URB transfert is complete (Isochronous pipe).
  309. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  310. * and not stall. Neat.
  311. */
  312. static void stk_isoc_handler(struct urb *urb)
  313. {
  314. int i;
  315. int ret;
  316. int framelen;
  317. unsigned long flags;
  318. unsigned char *fill = NULL;
  319. unsigned char *iso_buf = NULL;
  320. struct stk_camera *dev;
  321. struct stk_sio_buffer *fb;
  322. dev = (struct stk_camera *) urb->context;
  323. if (dev == NULL) {
  324. STK_ERROR("isoc_handler called with NULL device !\n");
  325. return;
  326. }
  327. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  328. || urb->status == -ESHUTDOWN) {
  329. atomic_dec(&dev->urbs_used);
  330. return;
  331. }
  332. spin_lock_irqsave(&dev->spinlock, flags);
  333. if (urb->status != -EINPROGRESS && urb->status != 0) {
  334. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  335. goto resubmit;
  336. }
  337. if (list_empty(&dev->sio_avail)) {
  338. /*FIXME Stop streaming after a while */
  339. (void) (printk_ratelimit() &&
  340. STK_ERROR("isoc_handler without available buffer!\n"));
  341. goto resubmit;
  342. }
  343. fb = list_first_entry(&dev->sio_avail,
  344. struct stk_sio_buffer, list);
  345. fill = fb->buffer + fb->v4lbuf.bytesused;
  346. for (i = 0; i < urb->number_of_packets; i++) {
  347. if (urb->iso_frame_desc[i].status != 0) {
  348. if (urb->iso_frame_desc[i].status != -EXDEV)
  349. STK_ERROR("Frame %d has error %d\n", i,
  350. urb->iso_frame_desc[i].status);
  351. continue;
  352. }
  353. framelen = urb->iso_frame_desc[i].actual_length;
  354. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  355. if (framelen <= 4)
  356. continue; /* no data */
  357. /*
  358. * we found something informational from there
  359. * the isoc frames have to type of headers
  360. * type1: 00 xx 00 00 or 20 xx 00 00
  361. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  362. * xx is a sequencer which has never been seen over 0x3f
  363. * imho data written down looks like bayer, i see similarities
  364. * after every 640 bytes
  365. */
  366. if (*iso_buf & 0x80) {
  367. framelen -= 8;
  368. iso_buf += 8;
  369. /* This marks a new frame */
  370. if (fb->v4lbuf.bytesused != 0
  371. && fb->v4lbuf.bytesused != dev->frame_size) {
  372. (void) (printk_ratelimit() &&
  373. STK_ERROR("frame %d, "
  374. "bytesused=%d, skipping\n",
  375. i, fb->v4lbuf.bytesused));
  376. fb->v4lbuf.bytesused = 0;
  377. fill = fb->buffer;
  378. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  379. if (list_is_singular(&dev->sio_avail)) {
  380. /* Always reuse the last buffer */
  381. fb->v4lbuf.bytesused = 0;
  382. fill = fb->buffer;
  383. } else {
  384. list_move_tail(dev->sio_avail.next,
  385. &dev->sio_full);
  386. wake_up(&dev->wait_frame);
  387. fb = list_first_entry(&dev->sio_avail,
  388. struct stk_sio_buffer, list);
  389. fb->v4lbuf.bytesused = 0;
  390. fill = fb->buffer;
  391. }
  392. }
  393. } else {
  394. framelen -= 4;
  395. iso_buf += 4;
  396. }
  397. /* Our buffer is full !!! */
  398. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  399. (void) (printk_ratelimit() &&
  400. STK_ERROR("Frame buffer overflow, lost sync\n"));
  401. /*FIXME Do something here? */
  402. continue;
  403. }
  404. spin_unlock_irqrestore(&dev->spinlock, flags);
  405. memcpy(fill, iso_buf, framelen);
  406. spin_lock_irqsave(&dev->spinlock, flags);
  407. fill += framelen;
  408. /* New size of our buffer */
  409. fb->v4lbuf.bytesused += framelen;
  410. }
  411. resubmit:
  412. spin_unlock_irqrestore(&dev->spinlock, flags);
  413. urb->dev = dev->udev;
  414. ret = usb_submit_urb(urb, GFP_ATOMIC);
  415. if (ret != 0) {
  416. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  417. ret);
  418. }
  419. }
  420. /* -------------------------------------------- */
  421. static int stk_prepare_iso(struct stk_camera *dev)
  422. {
  423. void *kbuf;
  424. int i, j;
  425. struct urb *urb;
  426. struct usb_device *udev;
  427. if (dev == NULL)
  428. return -ENXIO;
  429. udev = dev->udev;
  430. if (dev->isobufs)
  431. STK_ERROR("isobufs already allocated. Bad\n");
  432. else
  433. dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
  434. GFP_KERNEL);
  435. if (dev->isobufs == NULL) {
  436. STK_ERROR("Unable to allocate iso buffers\n");
  437. return -ENOMEM;
  438. }
  439. for (i = 0; i < MAX_ISO_BUFS; i++) {
  440. if (dev->isobufs[i].data == NULL) {
  441. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  442. if (kbuf == NULL) {
  443. STK_ERROR("Failed to allocate iso buffer %d\n",
  444. i);
  445. goto isobufs_out;
  446. }
  447. dev->isobufs[i].data = kbuf;
  448. } else
  449. STK_ERROR("isobuf data already allocated\n");
  450. if (dev->isobufs[i].urb == NULL) {
  451. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  452. if (urb == NULL) {
  453. STK_ERROR("Failed to allocate URB %d\n", i);
  454. goto isobufs_out;
  455. }
  456. dev->isobufs[i].urb = urb;
  457. } else {
  458. STK_ERROR("Killing URB\n");
  459. usb_kill_urb(dev->isobufs[i].urb);
  460. urb = dev->isobufs[i].urb;
  461. }
  462. urb->interval = 1;
  463. urb->dev = udev;
  464. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  465. urb->transfer_flags = URB_ISO_ASAP;
  466. urb->transfer_buffer = dev->isobufs[i].data;
  467. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  468. urb->complete = stk_isoc_handler;
  469. urb->context = dev;
  470. urb->start_frame = 0;
  471. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  472. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  473. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  474. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  475. }
  476. }
  477. set_memallocd(dev);
  478. return 0;
  479. isobufs_out:
  480. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  481. kfree(dev->isobufs[i].data);
  482. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  483. usb_free_urb(dev->isobufs[i].urb);
  484. kfree(dev->isobufs);
  485. dev->isobufs = NULL;
  486. return -ENOMEM;
  487. }
  488. static void stk_clean_iso(struct stk_camera *dev)
  489. {
  490. int i;
  491. if (dev == NULL || dev->isobufs == NULL)
  492. return;
  493. for (i = 0; i < MAX_ISO_BUFS; i++) {
  494. struct urb *urb;
  495. urb = dev->isobufs[i].urb;
  496. if (urb) {
  497. if (atomic_read(&dev->urbs_used))
  498. usb_kill_urb(urb);
  499. usb_free_urb(urb);
  500. }
  501. kfree(dev->isobufs[i].data);
  502. }
  503. kfree(dev->isobufs);
  504. dev->isobufs = NULL;
  505. unset_memallocd(dev);
  506. }
  507. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  508. {
  509. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  510. INIT_LIST_HEAD(&buf->list);
  511. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  512. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  513. if (buf->buffer == NULL)
  514. return -ENOMEM;
  515. buf->mapcount = 0;
  516. buf->dev = dev;
  517. buf->v4lbuf.index = index;
  518. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  519. buf->v4lbuf.field = V4L2_FIELD_NONE;
  520. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  521. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  522. return 0;
  523. }
  524. static int stk_free_sio_buffers(struct stk_camera *dev)
  525. {
  526. int i;
  527. int nbufs;
  528. unsigned long flags;
  529. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  530. return 0;
  531. /*
  532. * If any buffers are mapped, we cannot free them at all.
  533. */
  534. for (i = 0; i < dev->n_sbufs; i++) {
  535. if (dev->sio_bufs[i].mapcount > 0)
  536. return -EBUSY;
  537. }
  538. /*
  539. * OK, let's do it.
  540. */
  541. spin_lock_irqsave(&dev->spinlock, flags);
  542. INIT_LIST_HEAD(&dev->sio_avail);
  543. INIT_LIST_HEAD(&dev->sio_full);
  544. nbufs = dev->n_sbufs;
  545. dev->n_sbufs = 0;
  546. spin_unlock_irqrestore(&dev->spinlock, flags);
  547. for (i = 0; i < nbufs; i++) {
  548. if (dev->sio_bufs[i].buffer != NULL)
  549. vfree(dev->sio_bufs[i].buffer);
  550. }
  551. kfree(dev->sio_bufs);
  552. dev->sio_bufs = NULL;
  553. return 0;
  554. }
  555. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  556. {
  557. int i;
  558. if (dev->sio_bufs != NULL)
  559. STK_ERROR("sio_bufs already allocated\n");
  560. else {
  561. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  562. GFP_KERNEL);
  563. if (dev->sio_bufs == NULL)
  564. return -ENOMEM;
  565. for (i = 0; i < n_sbufs; i++) {
  566. if (stk_setup_siobuf(dev, i))
  567. return (dev->n_sbufs > 1)? 0 : -ENOMEM;
  568. dev->n_sbufs = i+1;
  569. }
  570. }
  571. return 0;
  572. }
  573. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  574. {
  575. int err;
  576. err = stk_prepare_iso(dev);
  577. if (err) {
  578. stk_clean_iso(dev);
  579. return err;
  580. }
  581. err = stk_prepare_sio_buffers(dev, n_sbufs);
  582. if (err) {
  583. stk_free_sio_buffers(dev);
  584. return err;
  585. }
  586. return 0;
  587. }
  588. static void stk_free_buffers(struct stk_camera *dev)
  589. {
  590. stk_clean_iso(dev);
  591. stk_free_sio_buffers(dev);
  592. }
  593. /* -------------------------------------------- */
  594. /* v4l file operations */
  595. static int v4l_stk_open(struct inode *inode, struct file *fp)
  596. {
  597. struct stk_camera *dev;
  598. struct video_device *vdev;
  599. vdev = video_devdata(fp);
  600. dev = vdev_to_camera(vdev);
  601. lock_kernel();
  602. if (dev == NULL || !is_present(dev)) {
  603. unlock_kernel();
  604. return -ENXIO;
  605. }
  606. fp->private_data = vdev;
  607. kref_get(&dev->kref);
  608. usb_autopm_get_interface(dev->interface);
  609. unlock_kernel();
  610. return 0;
  611. }
  612. static int v4l_stk_release(struct inode *inode, struct file *fp)
  613. {
  614. struct stk_camera *dev;
  615. struct video_device *vdev;
  616. vdev = video_devdata(fp);
  617. if (vdev == NULL) {
  618. STK_ERROR("v4l_release called w/o video devdata\n");
  619. return -EFAULT;
  620. }
  621. dev = vdev_to_camera(vdev);
  622. if (dev == NULL) {
  623. STK_ERROR("v4l_release called on removed device\n");
  624. return -ENODEV;
  625. }
  626. if (dev->owner != fp) {
  627. usb_autopm_put_interface(dev->interface);
  628. kref_put(&dev->kref, stk_camera_cleanup);
  629. return 0;
  630. }
  631. stk_stop_stream(dev);
  632. stk_free_buffers(dev);
  633. dev->owner = NULL;
  634. usb_autopm_put_interface(dev->interface);
  635. kref_put(&dev->kref, stk_camera_cleanup);
  636. return 0;
  637. }
  638. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  639. size_t count, loff_t *f_pos)
  640. {
  641. int i;
  642. int ret;
  643. unsigned long flags;
  644. struct stk_camera *dev;
  645. struct video_device *vdev;
  646. struct stk_sio_buffer *sbuf;
  647. vdev = video_devdata(fp);
  648. if (vdev == NULL)
  649. return -EFAULT;
  650. dev = vdev_to_camera(vdev);
  651. if (dev == NULL)
  652. return -EIO;
  653. if (!is_present(dev))
  654. return -EIO;
  655. if (dev->owner && dev->owner != fp)
  656. return -EBUSY;
  657. dev->owner = fp;
  658. if (!is_streaming(dev)) {
  659. if (stk_initialise(dev)
  660. || stk_allocate_buffers(dev, 3)
  661. || stk_start_stream(dev))
  662. return -ENOMEM;
  663. spin_lock_irqsave(&dev->spinlock, flags);
  664. for (i = 0; i < dev->n_sbufs; i++) {
  665. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  666. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  667. }
  668. spin_unlock_irqrestore(&dev->spinlock, flags);
  669. }
  670. if (*f_pos == 0) {
  671. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  672. return -EWOULDBLOCK;
  673. ret = wait_event_interruptible(dev->wait_frame,
  674. !list_empty(&dev->sio_full) || !is_present(dev));
  675. if (ret)
  676. return ret;
  677. if (!is_present(dev))
  678. return -EIO;
  679. }
  680. if (count + *f_pos > dev->frame_size)
  681. count = dev->frame_size - *f_pos;
  682. spin_lock_irqsave(&dev->spinlock, flags);
  683. if (list_empty(&dev->sio_full)) {
  684. spin_unlock_irqrestore(&dev->spinlock, flags);
  685. STK_ERROR("BUG: No siobufs ready\n");
  686. return 0;
  687. }
  688. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  689. spin_unlock_irqrestore(&dev->spinlock, flags);
  690. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  691. return -EFAULT;
  692. *f_pos += count;
  693. if (*f_pos >= dev->frame_size) {
  694. *f_pos = 0;
  695. spin_lock_irqsave(&dev->spinlock, flags);
  696. list_move_tail(&sbuf->list, &dev->sio_avail);
  697. spin_unlock_irqrestore(&dev->spinlock, flags);
  698. }
  699. return count;
  700. }
  701. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  702. {
  703. struct stk_camera *dev;
  704. struct video_device *vdev;
  705. vdev = video_devdata(fp);
  706. if (vdev == NULL)
  707. return -EFAULT;
  708. dev = vdev_to_camera(vdev);
  709. if (dev == NULL)
  710. return -ENODEV;
  711. poll_wait(fp, &dev->wait_frame, wait);
  712. if (!is_present(dev))
  713. return POLLERR;
  714. if (!list_empty(&dev->sio_full))
  715. return (POLLIN | POLLRDNORM);
  716. return 0;
  717. }
  718. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  719. {
  720. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  721. sbuf->mapcount++;
  722. }
  723. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  724. {
  725. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  726. sbuf->mapcount--;
  727. if (sbuf->mapcount == 0)
  728. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  729. }
  730. static struct vm_operations_struct stk_v4l_vm_ops = {
  731. .open = stk_v4l_vm_open,
  732. .close = stk_v4l_vm_close
  733. };
  734. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  735. {
  736. unsigned int i;
  737. int ret;
  738. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  739. struct stk_camera *dev;
  740. struct video_device *vdev;
  741. struct stk_sio_buffer *sbuf = NULL;
  742. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  743. return -EINVAL;
  744. vdev = video_devdata(fp);
  745. dev = vdev_to_camera(vdev);
  746. for (i = 0; i < dev->n_sbufs; i++) {
  747. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  748. sbuf = dev->sio_bufs + i;
  749. break;
  750. }
  751. }
  752. if (sbuf == NULL)
  753. return -EINVAL;
  754. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  755. if (ret)
  756. return ret;
  757. vma->vm_flags |= VM_DONTEXPAND;
  758. vma->vm_private_data = sbuf;
  759. vma->vm_ops = &stk_v4l_vm_ops;
  760. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  761. stk_v4l_vm_open(vma);
  762. return 0;
  763. }
  764. /* v4l ioctl handlers */
  765. static int stk_vidioc_querycap(struct file *filp,
  766. void *priv, struct v4l2_capability *cap)
  767. {
  768. strcpy(cap->driver, "stk");
  769. strcpy(cap->card, "stk");
  770. cap->version = DRIVER_VERSION_NUM;
  771. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  772. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  773. return 0;
  774. }
  775. static int stk_vidioc_enum_input(struct file *filp,
  776. void *priv, struct v4l2_input *input)
  777. {
  778. if (input->index != 0)
  779. return -EINVAL;
  780. strcpy(input->name, "Syntek USB Camera");
  781. input->type = V4L2_INPUT_TYPE_CAMERA;
  782. return 0;
  783. }
  784. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  785. {
  786. *i = 0;
  787. return 0;
  788. }
  789. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  790. {
  791. if (i != 0)
  792. return -EINVAL;
  793. else
  794. return 0;
  795. }
  796. /* from vivi.c */
  797. static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  798. {
  799. return 0;
  800. }
  801. /* List of all V4Lv2 controls supported by the driver */
  802. static struct v4l2_queryctrl stk_controls[] = {
  803. {
  804. .id = V4L2_CID_BRIGHTNESS,
  805. .type = V4L2_CTRL_TYPE_INTEGER,
  806. .name = "Brightness",
  807. .minimum = 0,
  808. .maximum = 0xffff,
  809. .step = 0x0100,
  810. .default_value = 0x6000,
  811. },
  812. /*TODO: get more controls to work */
  813. };
  814. static int stk_vidioc_queryctrl(struct file *filp,
  815. void *priv, struct v4l2_queryctrl *c)
  816. {
  817. int i;
  818. int nbr;
  819. nbr = ARRAY_SIZE(stk_controls);
  820. for (i = 0; i < nbr; i++) {
  821. if (stk_controls[i].id == c->id) {
  822. memcpy(c, &stk_controls[i],
  823. sizeof(struct v4l2_queryctrl));
  824. return 0;
  825. }
  826. }
  827. return -EINVAL;
  828. }
  829. static int stk_vidioc_g_ctrl(struct file *filp,
  830. void *priv, struct v4l2_control *c)
  831. {
  832. struct stk_camera *dev = priv;
  833. switch (c->id) {
  834. case V4L2_CID_BRIGHTNESS:
  835. c->value = dev->vsettings.brightness;
  836. break;
  837. default:
  838. return -EINVAL;
  839. }
  840. return 0;
  841. }
  842. static int stk_vidioc_s_ctrl(struct file *filp,
  843. void *priv, struct v4l2_control *c)
  844. {
  845. struct stk_camera *dev = priv;
  846. switch (c->id) {
  847. case V4L2_CID_BRIGHTNESS:
  848. dev->vsettings.brightness = c->value;
  849. return stk_sensor_set_brightness(dev, c->value >> 8);
  850. default:
  851. return -EINVAL;
  852. }
  853. return 0;
  854. }
  855. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  856. void *priv, struct v4l2_fmtdesc *fmtd)
  857. {
  858. fmtd->flags = 0;
  859. switch (fmtd->index) {
  860. case 0:
  861. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  862. strcpy(fmtd->description, "r5g6b5");
  863. break;
  864. case 1:
  865. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  866. strcpy(fmtd->description, "r5g6b5BE");
  867. break;
  868. case 2:
  869. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  870. strcpy(fmtd->description, "yuv4:2:2");
  871. break;
  872. case 3:
  873. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  874. strcpy(fmtd->description, "Raw bayer");
  875. break;
  876. case 4:
  877. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  878. strcpy(fmtd->description, "yuv4:2:2");
  879. break;
  880. default:
  881. return -EINVAL;
  882. }
  883. return 0;
  884. }
  885. static struct stk_size {
  886. unsigned w;
  887. unsigned h;
  888. enum stk_mode m;
  889. } stk_sizes[] = {
  890. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  891. { .w = 640, .h = 480, .m = MODE_VGA, },
  892. { .w = 352, .h = 288, .m = MODE_CIF, },
  893. { .w = 320, .h = 240, .m = MODE_QVGA, },
  894. { .w = 176, .h = 144, .m = MODE_QCIF, },
  895. };
  896. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  897. void *priv, struct v4l2_format *f)
  898. {
  899. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  900. struct stk_camera *dev = priv;
  901. int i;
  902. for (i = 0; i < ARRAY_SIZE(stk_sizes)
  903. && stk_sizes[i].m != dev->vsettings.mode;
  904. i++);
  905. if (i == ARRAY_SIZE(stk_sizes)) {
  906. STK_ERROR("ERROR: mode invalid\n");
  907. return -EINVAL;
  908. }
  909. pix_format->width = stk_sizes[i].w;
  910. pix_format->height = stk_sizes[i].h;
  911. pix_format->field = V4L2_FIELD_NONE;
  912. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  913. pix_format->priv = 0;
  914. pix_format->pixelformat = dev->vsettings.palette;
  915. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  916. pix_format->bytesperline = pix_format->width;
  917. else
  918. pix_format->bytesperline = 2 * pix_format->width;
  919. pix_format->sizeimage = pix_format->bytesperline
  920. * pix_format->height;
  921. return 0;
  922. }
  923. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  924. void *priv, struct v4l2_format *fmtd)
  925. {
  926. int i;
  927. switch (fmtd->fmt.pix.pixelformat) {
  928. case V4L2_PIX_FMT_RGB565:
  929. case V4L2_PIX_FMT_RGB565X:
  930. case V4L2_PIX_FMT_UYVY:
  931. case V4L2_PIX_FMT_YUYV:
  932. case V4L2_PIX_FMT_SBGGR8:
  933. break;
  934. default:
  935. return -EINVAL;
  936. }
  937. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  938. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  939. break;
  940. }
  941. if (i == ARRAY_SIZE(stk_sizes)
  942. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  943. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  944. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  945. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  946. fmtd->fmt.pix.priv = i - 1;
  947. } else {
  948. fmtd->fmt.pix.height = stk_sizes[i].h;
  949. fmtd->fmt.pix.width = stk_sizes[i].w;
  950. fmtd->fmt.pix.priv = i;
  951. }
  952. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  953. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  954. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  955. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  956. else
  957. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  958. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  959. * fmtd->fmt.pix.height;
  960. return 0;
  961. }
  962. static int stk_setup_format(struct stk_camera *dev)
  963. {
  964. int i = 0;
  965. int depth;
  966. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  967. depth = 1;
  968. else
  969. depth = 2;
  970. while (stk_sizes[i].m != dev->vsettings.mode
  971. && i < ARRAY_SIZE(stk_sizes))
  972. i++;
  973. if (i == ARRAY_SIZE(stk_sizes)) {
  974. STK_ERROR("Something is broken in %s\n", __func__);
  975. return -EFAULT;
  976. }
  977. /* This registers controls some timings, not sure of what. */
  978. stk_camera_write_reg(dev, 0x001b, 0x0e);
  979. if (dev->vsettings.mode == MODE_SXGA)
  980. stk_camera_write_reg(dev, 0x001c, 0x0e);
  981. else
  982. stk_camera_write_reg(dev, 0x001c, 0x46);
  983. /*
  984. * Registers 0x0115 0x0114 are the size of each line (bytes),
  985. * regs 0x0117 0x0116 are the heigth of the image.
  986. */
  987. stk_camera_write_reg(dev, 0x0115,
  988. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  989. stk_camera_write_reg(dev, 0x0114,
  990. (stk_sizes[i].w * depth) & 0xff);
  991. stk_camera_write_reg(dev, 0x0117,
  992. (stk_sizes[i].h >> 8) & 0xff);
  993. stk_camera_write_reg(dev, 0x0116,
  994. stk_sizes[i].h & 0xff);
  995. return stk_sensor_configure(dev);
  996. }
  997. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  998. void *priv, struct v4l2_format *fmtd)
  999. {
  1000. int ret;
  1001. struct stk_camera *dev = priv;
  1002. if (dev == NULL)
  1003. return -ENODEV;
  1004. if (!is_present(dev))
  1005. return -ENODEV;
  1006. if (is_streaming(dev))
  1007. return -EBUSY;
  1008. if (dev->owner && dev->owner != filp)
  1009. return -EBUSY;
  1010. ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
  1011. if (ret)
  1012. return ret;
  1013. dev->owner = filp;
  1014. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  1015. stk_free_buffers(dev);
  1016. dev->frame_size = fmtd->fmt.pix.sizeimage;
  1017. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  1018. stk_initialise(dev);
  1019. return stk_setup_format(dev);
  1020. }
  1021. static int stk_vidioc_reqbufs(struct file *filp,
  1022. void *priv, struct v4l2_requestbuffers *rb)
  1023. {
  1024. struct stk_camera *dev = priv;
  1025. if (dev == NULL)
  1026. return -ENODEV;
  1027. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1028. return -EINVAL;
  1029. if (rb->memory != V4L2_MEMORY_MMAP)
  1030. return -EINVAL;
  1031. if (is_streaming(dev)
  1032. || (dev->owner && dev->owner != filp))
  1033. return -EBUSY;
  1034. dev->owner = filp;
  1035. /*FIXME If they ask for zero, we must stop streaming and free */
  1036. if (rb->count < 3)
  1037. rb->count = 3;
  1038. /* Arbitrary limit */
  1039. else if (rb->count > 5)
  1040. rb->count = 5;
  1041. stk_allocate_buffers(dev, rb->count);
  1042. rb->count = dev->n_sbufs;
  1043. return 0;
  1044. }
  1045. static int stk_vidioc_querybuf(struct file *filp,
  1046. void *priv, struct v4l2_buffer *buf)
  1047. {
  1048. int index;
  1049. struct stk_camera *dev = priv;
  1050. struct stk_sio_buffer *sbuf;
  1051. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1052. return -EINVAL;
  1053. index = buf->index;
  1054. if (index < 0 || index >= dev->n_sbufs)
  1055. return -EINVAL;
  1056. sbuf = dev->sio_bufs + buf->index;
  1057. *buf = sbuf->v4lbuf;
  1058. return 0;
  1059. }
  1060. static int stk_vidioc_qbuf(struct file *filp,
  1061. void *priv, struct v4l2_buffer *buf)
  1062. {
  1063. struct stk_camera *dev = priv;
  1064. struct stk_sio_buffer *sbuf;
  1065. unsigned long flags;
  1066. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1067. return -EINVAL;
  1068. if (buf->memory != V4L2_MEMORY_MMAP)
  1069. return -EINVAL;
  1070. if (buf->index < 0 || buf->index >= dev->n_sbufs)
  1071. return -EINVAL;
  1072. sbuf = dev->sio_bufs + buf->index;
  1073. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  1074. return 0;
  1075. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1076. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1077. spin_lock_irqsave(&dev->spinlock, flags);
  1078. list_add_tail(&sbuf->list, &dev->sio_avail);
  1079. *buf = sbuf->v4lbuf;
  1080. spin_unlock_irqrestore(&dev->spinlock, flags);
  1081. return 0;
  1082. }
  1083. static int stk_vidioc_dqbuf(struct file *filp,
  1084. void *priv, struct v4l2_buffer *buf)
  1085. {
  1086. struct stk_camera *dev = priv;
  1087. struct stk_sio_buffer *sbuf;
  1088. unsigned long flags;
  1089. int ret;
  1090. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1091. || !is_streaming(dev))
  1092. return -EINVAL;
  1093. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  1094. return -EWOULDBLOCK;
  1095. ret = wait_event_interruptible(dev->wait_frame,
  1096. !list_empty(&dev->sio_full) || !is_present(dev));
  1097. if (ret)
  1098. return ret;
  1099. if (!is_present(dev))
  1100. return -EIO;
  1101. spin_lock_irqsave(&dev->spinlock, flags);
  1102. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  1103. list_del_init(&sbuf->list);
  1104. spin_unlock_irqrestore(&dev->spinlock, flags);
  1105. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1106. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1107. sbuf->v4lbuf.sequence = ++dev->sequence;
  1108. do_gettimeofday(&sbuf->v4lbuf.timestamp);
  1109. *buf = sbuf->v4lbuf;
  1110. return 0;
  1111. }
  1112. static int stk_vidioc_streamon(struct file *filp,
  1113. void *priv, enum v4l2_buf_type type)
  1114. {
  1115. struct stk_camera *dev = priv;
  1116. if (is_streaming(dev))
  1117. return 0;
  1118. if (dev->sio_bufs == NULL)
  1119. return -EINVAL;
  1120. dev->sequence = 0;
  1121. return stk_start_stream(dev);
  1122. }
  1123. static int stk_vidioc_streamoff(struct file *filp,
  1124. void *priv, enum v4l2_buf_type type)
  1125. {
  1126. struct stk_camera *dev = priv;
  1127. unsigned long flags;
  1128. int i;
  1129. stk_stop_stream(dev);
  1130. spin_lock_irqsave(&dev->spinlock, flags);
  1131. INIT_LIST_HEAD(&dev->sio_avail);
  1132. INIT_LIST_HEAD(&dev->sio_full);
  1133. for (i = 0; i < dev->n_sbufs; i++) {
  1134. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1135. dev->sio_bufs[i].v4lbuf.flags = 0;
  1136. }
  1137. spin_unlock_irqrestore(&dev->spinlock, flags);
  1138. return 0;
  1139. }
  1140. static int stk_vidioc_g_parm(struct file *filp,
  1141. void *priv, struct v4l2_streamparm *sp)
  1142. {
  1143. if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1144. return -EINVAL;
  1145. sp->parm.capture.capability = 0;
  1146. sp->parm.capture.capturemode = 0;
  1147. /*FIXME This is not correct */
  1148. sp->parm.capture.timeperframe.numerator = 1;
  1149. sp->parm.capture.timeperframe.denominator = 30;
  1150. sp->parm.capture.readbuffers = 2;
  1151. sp->parm.capture.extendedmode = 0;
  1152. return 0;
  1153. }
  1154. static struct file_operations v4l_stk_fops = {
  1155. .owner = THIS_MODULE,
  1156. .open = v4l_stk_open,
  1157. .release = v4l_stk_release,
  1158. .read = v4l_stk_read,
  1159. .poll = v4l_stk_poll,
  1160. .mmap = v4l_stk_mmap,
  1161. .ioctl = video_ioctl2,
  1162. #ifdef CONFIG_COMPAT
  1163. .compat_ioctl = v4l_compat_ioctl32,
  1164. #endif
  1165. .llseek = no_llseek
  1166. };
  1167. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1168. .vidioc_querycap = stk_vidioc_querycap,
  1169. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1170. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1171. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1172. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1173. .vidioc_enum_input = stk_vidioc_enum_input,
  1174. .vidioc_s_input = stk_vidioc_s_input,
  1175. .vidioc_g_input = stk_vidioc_g_input,
  1176. .vidioc_s_std = stk_vidioc_s_std,
  1177. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1178. .vidioc_querybuf = stk_vidioc_querybuf,
  1179. .vidioc_qbuf = stk_vidioc_qbuf,
  1180. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1181. .vidioc_streamon = stk_vidioc_streamon,
  1182. .vidioc_streamoff = stk_vidioc_streamoff,
  1183. .vidioc_queryctrl = stk_vidioc_queryctrl,
  1184. .vidioc_g_ctrl = stk_vidioc_g_ctrl,
  1185. .vidioc_s_ctrl = stk_vidioc_s_ctrl,
  1186. .vidioc_g_parm = stk_vidioc_g_parm,
  1187. };
  1188. static void stk_v4l_dev_release(struct video_device *vd)
  1189. {
  1190. }
  1191. static struct video_device stk_v4l_data = {
  1192. .name = "stkwebcam",
  1193. .minor = -1,
  1194. .tvnorms = V4L2_STD_UNKNOWN,
  1195. .current_norm = V4L2_STD_UNKNOWN,
  1196. .fops = &v4l_stk_fops,
  1197. .ioctl_ops = &v4l_stk_ioctl_ops,
  1198. .release = stk_v4l_dev_release,
  1199. };
  1200. static int stk_register_video_device(struct stk_camera *dev)
  1201. {
  1202. int err;
  1203. dev->vdev = stk_v4l_data;
  1204. dev->vdev.debug = debug;
  1205. dev->vdev.parent = &dev->interface->dev;
  1206. dev->vdev.priv = dev;
  1207. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1208. if (err)
  1209. STK_ERROR("v4l registration failed\n");
  1210. else
  1211. STK_INFO("Syntek USB2.0 Camera is now controlling video device"
  1212. " /dev/video%d\n", dev->vdev.minor);
  1213. return err;
  1214. }
  1215. /* USB Stuff */
  1216. static int stk_camera_probe(struct usb_interface *interface,
  1217. const struct usb_device_id *id)
  1218. {
  1219. int i;
  1220. int err;
  1221. struct stk_camera *dev = NULL;
  1222. struct usb_device *udev = interface_to_usbdev(interface);
  1223. struct usb_host_interface *iface_desc;
  1224. struct usb_endpoint_descriptor *endpoint;
  1225. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1226. if (dev == NULL) {
  1227. STK_ERROR("Out of memory !\n");
  1228. return -ENOMEM;
  1229. }
  1230. kref_init(&dev->kref);
  1231. spin_lock_init(&dev->spinlock);
  1232. init_waitqueue_head(&dev->wait_frame);
  1233. dev->udev = udev;
  1234. dev->interface = interface;
  1235. usb_get_intf(interface);
  1236. dev->vsettings.vflip = vflip;
  1237. dev->vsettings.hflip = hflip;
  1238. dev->n_sbufs = 0;
  1239. set_present(dev);
  1240. /* Set up the endpoint information
  1241. * use only the first isoc-in endpoint
  1242. * for the current alternate setting */
  1243. iface_desc = interface->cur_altsetting;
  1244. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1245. endpoint = &iface_desc->endpoint[i].desc;
  1246. if (!dev->isoc_ep
  1247. && ((endpoint->bEndpointAddress
  1248. & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1249. && ((endpoint->bmAttributes
  1250. & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
  1251. /* we found an isoc in endpoint */
  1252. dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
  1253. break;
  1254. }
  1255. }
  1256. if (!dev->isoc_ep) {
  1257. STK_ERROR("Could not find isoc-in endpoint");
  1258. kref_put(&dev->kref, stk_camera_cleanup);
  1259. return -ENODEV;
  1260. }
  1261. dev->vsettings.brightness = 0x7fff;
  1262. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1263. dev->vsettings.mode = MODE_VGA;
  1264. dev->frame_size = 640 * 480 * 2;
  1265. INIT_LIST_HEAD(&dev->sio_avail);
  1266. INIT_LIST_HEAD(&dev->sio_full);
  1267. usb_set_intfdata(interface, dev);
  1268. err = stk_register_video_device(dev);
  1269. if (err) {
  1270. kref_put(&dev->kref, stk_camera_cleanup);
  1271. return err;
  1272. }
  1273. stk_create_sysfs_files(&dev->vdev);
  1274. usb_autopm_enable(dev->interface);
  1275. return 0;
  1276. }
  1277. static void stk_camera_disconnect(struct usb_interface *interface)
  1278. {
  1279. struct stk_camera *dev = usb_get_intfdata(interface);
  1280. usb_set_intfdata(interface, NULL);
  1281. unset_present(dev);
  1282. wake_up_interruptible(&dev->wait_frame);
  1283. stk_remove_sysfs_files(&dev->vdev);
  1284. kref_put(&dev->kref, stk_camera_cleanup);
  1285. }
  1286. #ifdef CONFIG_PM
  1287. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1288. {
  1289. struct stk_camera *dev = usb_get_intfdata(intf);
  1290. if (is_streaming(dev)) {
  1291. stk_stop_stream(dev);
  1292. /* yes, this is ugly */
  1293. set_streaming(dev);
  1294. }
  1295. return 0;
  1296. }
  1297. static int stk_camera_resume(struct usb_interface *intf)
  1298. {
  1299. struct stk_camera *dev = usb_get_intfdata(intf);
  1300. if (!is_initialised(dev))
  1301. return 0;
  1302. unset_initialised(dev);
  1303. stk_initialise(dev);
  1304. stk_setup_format(dev);
  1305. if (is_streaming(dev))
  1306. stk_start_stream(dev);
  1307. return 0;
  1308. }
  1309. #endif
  1310. static struct usb_driver stk_camera_driver = {
  1311. .name = "stkwebcam",
  1312. .probe = stk_camera_probe,
  1313. .disconnect = stk_camera_disconnect,
  1314. .id_table = stkwebcam_table,
  1315. #ifdef CONFIG_PM
  1316. .suspend = stk_camera_suspend,
  1317. .resume = stk_camera_resume,
  1318. #endif
  1319. };
  1320. static int __init stk_camera_init(void)
  1321. {
  1322. int result;
  1323. result = usb_register(&stk_camera_driver);
  1324. if (result)
  1325. STK_ERROR("usb_register failed ! Error number %d\n", result);
  1326. return result;
  1327. }
  1328. static void __exit stk_camera_exit(void)
  1329. {
  1330. usb_deregister(&stk_camera_driver);
  1331. }
  1332. module_init(stk_camera_init);
  1333. module_exit(stk_camera_exit);