stk-webcam.c 35 KB

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