stk-webcam.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. static 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. usb_autopm_get_interface(dev->interface);
  596. return 0;
  597. }
  598. static int v4l_stk_release(struct inode *inode, struct file *fp)
  599. {
  600. struct stk_camera *dev;
  601. struct video_device *vdev;
  602. vdev = video_devdata(fp);
  603. if (vdev == NULL) {
  604. STK_ERROR("v4l_release called w/o video devdata\n");
  605. return -EFAULT;
  606. }
  607. dev = vdev_to_camera(vdev);
  608. if (dev == NULL) {
  609. STK_ERROR("v4l_release called on removed device\n");
  610. return -ENODEV;
  611. }
  612. if (dev->owner != fp) {
  613. usb_autopm_put_interface(dev->interface);
  614. kref_put(&dev->kref, stk_camera_cleanup);
  615. return 0;
  616. }
  617. stk_stop_stream(dev);
  618. stk_free_buffers(dev);
  619. dev->owner = NULL;
  620. usb_autopm_put_interface(dev->interface);
  621. kref_put(&dev->kref, stk_camera_cleanup);
  622. return 0;
  623. }
  624. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  625. size_t count, loff_t *f_pos)
  626. {
  627. int i;
  628. int ret;
  629. unsigned long flags;
  630. struct stk_camera *dev;
  631. struct video_device *vdev;
  632. struct stk_sio_buffer *sbuf;
  633. vdev = video_devdata(fp);
  634. if (vdev == NULL)
  635. return -EFAULT;
  636. dev = vdev_to_camera(vdev);
  637. if (dev == NULL)
  638. return -EIO;
  639. if (!is_present(dev))
  640. return -EIO;
  641. if (dev->owner && dev->owner != fp)
  642. return -EBUSY;
  643. dev->owner = fp;
  644. if (!is_streaming(dev)) {
  645. if (stk_initialise(dev)
  646. || stk_allocate_buffers(dev, 3)
  647. || stk_start_stream(dev))
  648. return -ENOMEM;
  649. spin_lock_irqsave(&dev->spinlock, flags);
  650. for (i = 0; i < dev->n_sbufs; i++) {
  651. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  652. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  653. }
  654. spin_unlock_irqrestore(&dev->spinlock, flags);
  655. }
  656. if (*f_pos == 0) {
  657. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  658. return -EWOULDBLOCK;
  659. ret = wait_event_interruptible(dev->wait_frame,
  660. !list_empty(&dev->sio_full) || !is_present(dev));
  661. if (ret)
  662. return ret;
  663. if (!is_present(dev))
  664. return -EIO;
  665. }
  666. if (count + *f_pos > dev->frame_size)
  667. count = dev->frame_size - *f_pos;
  668. spin_lock_irqsave(&dev->spinlock, flags);
  669. if (list_empty(&dev->sio_full)) {
  670. spin_unlock_irqrestore(&dev->spinlock, flags);
  671. STK_ERROR("BUG: No siobufs ready\n");
  672. return 0;
  673. }
  674. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  675. spin_unlock_irqrestore(&dev->spinlock, flags);
  676. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  677. return -EFAULT;
  678. *f_pos += count;
  679. if (*f_pos >= dev->frame_size) {
  680. *f_pos = 0;
  681. spin_lock_irqsave(&dev->spinlock, flags);
  682. list_move_tail(&sbuf->list, &dev->sio_avail);
  683. spin_unlock_irqrestore(&dev->spinlock, flags);
  684. }
  685. return count;
  686. }
  687. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  688. {
  689. struct stk_camera *dev;
  690. struct video_device *vdev;
  691. vdev = video_devdata(fp);
  692. if (vdev == NULL)
  693. return -EFAULT;
  694. dev = vdev_to_camera(vdev);
  695. if (dev == NULL)
  696. return -ENODEV;
  697. poll_wait(fp, &dev->wait_frame, wait);
  698. if (!is_present(dev))
  699. return POLLERR;
  700. if (!list_empty(&dev->sio_full))
  701. return (POLLIN | POLLRDNORM);
  702. return 0;
  703. }
  704. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  705. {
  706. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  707. sbuf->mapcount++;
  708. }
  709. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  710. {
  711. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  712. sbuf->mapcount--;
  713. if (sbuf->mapcount == 0)
  714. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  715. }
  716. static struct vm_operations_struct stk_v4l_vm_ops = {
  717. .open = stk_v4l_vm_open,
  718. .close = stk_v4l_vm_close
  719. };
  720. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  721. {
  722. unsigned int i;
  723. int ret;
  724. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  725. struct stk_camera *dev;
  726. struct video_device *vdev;
  727. struct stk_sio_buffer *sbuf = NULL;
  728. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  729. return -EINVAL;
  730. vdev = video_devdata(fp);
  731. dev = vdev_to_camera(vdev);
  732. for (i = 0; i < dev->n_sbufs; i++) {
  733. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  734. sbuf = dev->sio_bufs + i;
  735. break;
  736. }
  737. }
  738. if (sbuf == NULL)
  739. return -EINVAL;
  740. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  741. if (ret)
  742. return ret;
  743. vma->vm_flags |= VM_DONTEXPAND;
  744. vma->vm_private_data = sbuf;
  745. vma->vm_ops = &stk_v4l_vm_ops;
  746. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  747. stk_v4l_vm_open(vma);
  748. return 0;
  749. }
  750. /* v4l ioctl handlers */
  751. static int stk_vidioc_querycap(struct file *filp,
  752. void *priv, struct v4l2_capability *cap)
  753. {
  754. strcpy(cap->driver, "stk");
  755. strcpy(cap->card, "stk");
  756. cap->version = DRIVER_VERSION_NUM;
  757. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  758. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  759. return 0;
  760. }
  761. static int stk_vidioc_enum_input(struct file *filp,
  762. void *priv, struct v4l2_input *input)
  763. {
  764. if (input->index != 0)
  765. return -EINVAL;
  766. strcpy(input->name, "Syntek USB Camera");
  767. input->type = V4L2_INPUT_TYPE_CAMERA;
  768. return 0;
  769. }
  770. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  771. {
  772. *i = 0;
  773. return 0;
  774. }
  775. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  776. {
  777. if (i != 0)
  778. return -EINVAL;
  779. else
  780. return 0;
  781. }
  782. /* from vivi.c */
  783. static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  784. {
  785. return 0;
  786. }
  787. /* List of all V4Lv2 controls supported by the driver */
  788. static struct v4l2_queryctrl stk_controls[] = {
  789. {
  790. .id = V4L2_CID_BRIGHTNESS,
  791. .type = V4L2_CTRL_TYPE_INTEGER,
  792. .name = "Brightness",
  793. .minimum = 0,
  794. .maximum = 0xffff,
  795. .step = 0x0100,
  796. .default_value = 0x6000,
  797. },
  798. /*TODO: get more controls to work */
  799. };
  800. static int stk_vidioc_queryctrl(struct file *filp,
  801. void *priv, struct v4l2_queryctrl *c)
  802. {
  803. int i;
  804. int nbr;
  805. nbr = ARRAY_SIZE(stk_controls);
  806. for (i = 0; i < nbr; i++) {
  807. if (stk_controls[i].id == c->id) {
  808. memcpy(c, &stk_controls[i],
  809. sizeof(struct v4l2_queryctrl));
  810. return 0;
  811. }
  812. }
  813. return -EINVAL;
  814. }
  815. static int stk_vidioc_g_ctrl(struct file *filp,
  816. void *priv, struct v4l2_control *c)
  817. {
  818. struct stk_camera *dev = priv;
  819. switch (c->id) {
  820. case V4L2_CID_BRIGHTNESS:
  821. c->value = dev->vsettings.brightness;
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. return 0;
  827. }
  828. static int stk_vidioc_s_ctrl(struct file *filp,
  829. void *priv, struct v4l2_control *c)
  830. {
  831. struct stk_camera *dev = priv;
  832. switch (c->id) {
  833. case V4L2_CID_BRIGHTNESS:
  834. dev->vsettings.brightness = c->value;
  835. return stk_sensor_set_brightness(dev, c->value >> 8);
  836. default:
  837. return -EINVAL;
  838. }
  839. return 0;
  840. }
  841. static int stk_vidioc_enum_fmt_cap(struct file *filp,
  842. void *priv, struct v4l2_fmtdesc *fmtd)
  843. {
  844. fmtd->flags = 0;
  845. switch (fmtd->index) {
  846. case 0:
  847. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  848. strcpy(fmtd->description, "r5g6b5");
  849. break;
  850. case 1:
  851. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  852. strcpy(fmtd->description, "r5g6b5BE");
  853. break;
  854. case 2:
  855. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  856. strcpy(fmtd->description, "yuv4:2:2");
  857. break;
  858. case 3:
  859. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  860. strcpy(fmtd->description, "Raw bayer");
  861. break;
  862. case 4:
  863. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  864. strcpy(fmtd->description, "yuv4:2:2");
  865. break;
  866. default:
  867. return -EINVAL;
  868. }
  869. return 0;
  870. }
  871. static struct stk_size {
  872. unsigned w;
  873. unsigned h;
  874. enum stk_mode m;
  875. } stk_sizes[] = {
  876. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  877. { .w = 640, .h = 480, .m = MODE_VGA, },
  878. { .w = 352, .h = 288, .m = MODE_CIF, },
  879. { .w = 320, .h = 240, .m = MODE_QVGA, },
  880. { .w = 176, .h = 144, .m = MODE_QCIF, },
  881. };
  882. static int stk_vidioc_g_fmt_cap(struct file *filp,
  883. void *priv, struct v4l2_format *f)
  884. {
  885. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  886. struct stk_camera *dev = priv;
  887. int i;
  888. for (i = 0; i < ARRAY_SIZE(stk_sizes)
  889. && stk_sizes[i].m != dev->vsettings.mode;
  890. i++);
  891. if (i == ARRAY_SIZE(stk_sizes)) {
  892. STK_ERROR("ERROR: mode invalid\n");
  893. return -EINVAL;
  894. }
  895. pix_format->width = stk_sizes[i].w;
  896. pix_format->height = stk_sizes[i].h;
  897. pix_format->field = V4L2_FIELD_NONE;
  898. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  899. pix_format->priv = 0;
  900. pix_format->pixelformat = dev->vsettings.palette;
  901. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  902. pix_format->bytesperline = pix_format->width;
  903. else
  904. pix_format->bytesperline = 2 * pix_format->width;
  905. pix_format->sizeimage = pix_format->bytesperline
  906. * pix_format->height;
  907. return 0;
  908. }
  909. static int stk_vidioc_try_fmt_cap(struct file *filp,
  910. void *priv, struct v4l2_format *fmtd)
  911. {
  912. int i;
  913. switch (fmtd->fmt.pix.pixelformat) {
  914. case V4L2_PIX_FMT_RGB565:
  915. case V4L2_PIX_FMT_RGB565X:
  916. case V4L2_PIX_FMT_UYVY:
  917. case V4L2_PIX_FMT_YUYV:
  918. case V4L2_PIX_FMT_SBGGR8:
  919. break;
  920. default:
  921. return -EINVAL;
  922. }
  923. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  924. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  925. break;
  926. }
  927. if (i == ARRAY_SIZE(stk_sizes)
  928. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  929. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  930. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  931. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  932. fmtd->fmt.pix.priv = i - 1;
  933. } else {
  934. fmtd->fmt.pix.height = stk_sizes[i].h;
  935. fmtd->fmt.pix.width = stk_sizes[i].w;
  936. fmtd->fmt.pix.priv = i;
  937. }
  938. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  939. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  940. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  941. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  942. else
  943. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  944. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  945. * fmtd->fmt.pix.height;
  946. return 0;
  947. }
  948. static int stk_setup_format(struct stk_camera *dev)
  949. {
  950. int i = 0;
  951. int depth;
  952. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  953. depth = 1;
  954. else
  955. depth = 2;
  956. while (stk_sizes[i].m != dev->vsettings.mode
  957. && i < ARRAY_SIZE(stk_sizes))
  958. i++;
  959. if (i == ARRAY_SIZE(stk_sizes)) {
  960. STK_ERROR("Something is broken in %s\n", __FUNCTION__);
  961. return -EFAULT;
  962. }
  963. /* This registers controls some timings, not sure of what. */
  964. stk_camera_write_reg(dev, 0x001b, 0x0e);
  965. if (dev->vsettings.mode == MODE_SXGA)
  966. stk_camera_write_reg(dev, 0x001c, 0x0e);
  967. else
  968. stk_camera_write_reg(dev, 0x001c, 0x46);
  969. /*
  970. * Registers 0x0115 0x0114 are the size of each line (bytes),
  971. * regs 0x0117 0x0116 are the heigth of the image.
  972. */
  973. stk_camera_write_reg(dev, 0x0115,
  974. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  975. stk_camera_write_reg(dev, 0x0114,
  976. (stk_sizes[i].w * depth) & 0xff);
  977. stk_camera_write_reg(dev, 0x0117,
  978. (stk_sizes[i].h >> 8) & 0xff);
  979. stk_camera_write_reg(dev, 0x0116,
  980. stk_sizes[i].h & 0xff);
  981. return stk_sensor_configure(dev);
  982. }
  983. static int stk_vidioc_s_fmt_cap(struct file *filp,
  984. void *priv, struct v4l2_format *fmtd)
  985. {
  986. int ret;
  987. struct stk_camera *dev = priv;
  988. if (dev == NULL)
  989. return -ENODEV;
  990. if (!is_present(dev))
  991. return -ENODEV;
  992. if (is_streaming(dev))
  993. return -EBUSY;
  994. if (dev->owner && dev->owner != filp)
  995. return -EBUSY;
  996. ret = stk_vidioc_try_fmt_cap(filp, priv, fmtd);
  997. if (ret)
  998. return ret;
  999. dev->owner = filp;
  1000. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  1001. stk_free_buffers(dev);
  1002. dev->frame_size = fmtd->fmt.pix.sizeimage;
  1003. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  1004. stk_initialise(dev);
  1005. return stk_setup_format(dev);
  1006. }
  1007. static int stk_vidioc_reqbufs(struct file *filp,
  1008. void *priv, struct v4l2_requestbuffers *rb)
  1009. {
  1010. struct stk_camera *dev = priv;
  1011. if (dev == NULL)
  1012. return -ENODEV;
  1013. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1014. return -EINVAL;
  1015. if (rb->memory != V4L2_MEMORY_MMAP)
  1016. return -EINVAL;
  1017. if (is_streaming(dev)
  1018. || (dev->owner && dev->owner != filp))
  1019. return -EBUSY;
  1020. dev->owner = filp;
  1021. /*FIXME If they ask for zero, we must stop streaming and free */
  1022. if (rb->count < 3)
  1023. rb->count = 3;
  1024. /* Arbitrary limit */
  1025. else if (rb->count > 5)
  1026. rb->count = 5;
  1027. stk_allocate_buffers(dev, rb->count);
  1028. rb->count = dev->n_sbufs;
  1029. return 0;
  1030. }
  1031. static int stk_vidioc_querybuf(struct file *filp,
  1032. void *priv, struct v4l2_buffer *buf)
  1033. {
  1034. int index;
  1035. struct stk_camera *dev = priv;
  1036. struct stk_sio_buffer *sbuf;
  1037. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1038. return -EINVAL;
  1039. index = buf->index;
  1040. if (index < 0 || index >= dev->n_sbufs)
  1041. return -EINVAL;
  1042. sbuf = dev->sio_bufs + buf->index;
  1043. *buf = sbuf->v4lbuf;
  1044. return 0;
  1045. }
  1046. static int stk_vidioc_qbuf(struct file *filp,
  1047. void *priv, struct v4l2_buffer *buf)
  1048. {
  1049. struct stk_camera *dev = priv;
  1050. struct stk_sio_buffer *sbuf;
  1051. unsigned long flags;
  1052. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1053. return -EINVAL;
  1054. if (buf->memory != V4L2_MEMORY_MMAP)
  1055. return -EINVAL;
  1056. if (buf->index < 0 || buf->index >= dev->n_sbufs)
  1057. return -EINVAL;
  1058. sbuf = dev->sio_bufs + buf->index;
  1059. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  1060. return 0;
  1061. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  1062. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  1063. spin_lock_irqsave(&dev->spinlock, flags);
  1064. list_add_tail(&sbuf->list, &dev->sio_avail);
  1065. *buf = sbuf->v4lbuf;
  1066. spin_unlock_irqrestore(&dev->spinlock, flags);
  1067. return 0;
  1068. }
  1069. static int stk_vidioc_dqbuf(struct file *filp,
  1070. void *priv, struct v4l2_buffer *buf)
  1071. {
  1072. struct stk_camera *dev = priv;
  1073. struct stk_sio_buffer *sbuf;
  1074. unsigned long flags;
  1075. int ret;
  1076. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1077. || !is_streaming(dev))
  1078. return -EINVAL;
  1079. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  1080. return -EWOULDBLOCK;
  1081. ret = wait_event_interruptible(dev->wait_frame,
  1082. !list_empty(&dev->sio_full) || !is_present(dev));
  1083. if (ret)
  1084. return ret;
  1085. if (!is_present(dev))
  1086. return -EIO;
  1087. spin_lock_irqsave(&dev->spinlock, flags);
  1088. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  1089. list_del_init(&sbuf->list);
  1090. spin_unlock_irqrestore(&dev->spinlock, flags);
  1091. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1092. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1093. sbuf->v4lbuf.sequence = ++dev->sequence;
  1094. do_gettimeofday(&sbuf->v4lbuf.timestamp);
  1095. *buf = sbuf->v4lbuf;
  1096. return 0;
  1097. }
  1098. static int stk_vidioc_streamon(struct file *filp,
  1099. void *priv, enum v4l2_buf_type type)
  1100. {
  1101. struct stk_camera *dev = priv;
  1102. if (is_streaming(dev))
  1103. return 0;
  1104. if (dev->sio_bufs == NULL)
  1105. return -EINVAL;
  1106. dev->sequence = 0;
  1107. return stk_start_stream(dev);
  1108. }
  1109. static int stk_vidioc_streamoff(struct file *filp,
  1110. void *priv, enum v4l2_buf_type type)
  1111. {
  1112. struct stk_camera *dev = priv;
  1113. unsigned long flags;
  1114. int i;
  1115. stk_stop_stream(dev);
  1116. spin_lock_irqsave(&dev->spinlock, flags);
  1117. INIT_LIST_HEAD(&dev->sio_avail);
  1118. INIT_LIST_HEAD(&dev->sio_full);
  1119. for (i = 0; i < dev->n_sbufs; i++) {
  1120. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1121. dev->sio_bufs[i].v4lbuf.flags = 0;
  1122. }
  1123. spin_unlock_irqrestore(&dev->spinlock, flags);
  1124. return 0;
  1125. }
  1126. static int stk_vidioc_g_parm(struct file *filp,
  1127. void *priv, struct v4l2_streamparm *sp)
  1128. {
  1129. if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1130. return -EINVAL;
  1131. sp->parm.capture.capability = 0;
  1132. sp->parm.capture.capturemode = 0;
  1133. /*FIXME This is not correct */
  1134. sp->parm.capture.timeperframe.numerator = 1;
  1135. sp->parm.capture.timeperframe.denominator = 30;
  1136. sp->parm.capture.readbuffers = 2;
  1137. sp->parm.capture.extendedmode = 0;
  1138. return 0;
  1139. }
  1140. static struct file_operations v4l_stk_fops = {
  1141. .owner = THIS_MODULE,
  1142. .open = v4l_stk_open,
  1143. .release = v4l_stk_release,
  1144. .read = v4l_stk_read,
  1145. .poll = v4l_stk_poll,
  1146. .mmap = v4l_stk_mmap,
  1147. .ioctl = video_ioctl2,
  1148. #ifdef CONFIG_COMPAT
  1149. .compat_ioctl = v4l_compat_ioctl32,
  1150. #endif
  1151. .llseek = no_llseek
  1152. };
  1153. static void stk_v4l_dev_release(struct video_device *vd)
  1154. {
  1155. }
  1156. static struct video_device stk_v4l_data = {
  1157. .name = "stkwebcam",
  1158. .type = VFL_TYPE_GRABBER,
  1159. .type2 = VID_TYPE_CAPTURE,
  1160. .minor = -1,
  1161. .tvnorms = V4L2_STD_UNKNOWN,
  1162. .current_norm = V4L2_STD_UNKNOWN,
  1163. .fops = &v4l_stk_fops,
  1164. .release = stk_v4l_dev_release,
  1165. .vidioc_querycap = stk_vidioc_querycap,
  1166. .vidioc_enum_fmt_cap = stk_vidioc_enum_fmt_cap,
  1167. .vidioc_try_fmt_cap = stk_vidioc_try_fmt_cap,
  1168. .vidioc_s_fmt_cap = stk_vidioc_s_fmt_cap,
  1169. .vidioc_g_fmt_cap = stk_vidioc_g_fmt_cap,
  1170. .vidioc_enum_input = stk_vidioc_enum_input,
  1171. .vidioc_s_input = stk_vidioc_s_input,
  1172. .vidioc_g_input = stk_vidioc_g_input,
  1173. .vidioc_s_std = stk_vidioc_s_std,
  1174. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1175. .vidioc_querybuf = stk_vidioc_querybuf,
  1176. .vidioc_qbuf = stk_vidioc_qbuf,
  1177. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1178. .vidioc_streamon = stk_vidioc_streamon,
  1179. .vidioc_streamoff = stk_vidioc_streamoff,
  1180. .vidioc_queryctrl = stk_vidioc_queryctrl,
  1181. .vidioc_g_ctrl = stk_vidioc_g_ctrl,
  1182. .vidioc_s_ctrl = stk_vidioc_s_ctrl,
  1183. .vidioc_g_parm = stk_vidioc_g_parm,
  1184. };
  1185. static int stk_register_video_device(struct stk_camera *dev)
  1186. {
  1187. int err;
  1188. dev->vdev = stk_v4l_data;
  1189. dev->vdev.debug = debug;
  1190. dev->vdev.dev = &dev->interface->dev;
  1191. dev->vdev.priv = dev;
  1192. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1193. if (err)
  1194. STK_ERROR("v4l registration failed\n");
  1195. else
  1196. STK_INFO("Syntek USB2.0 Camera is now controlling video device"
  1197. " /dev/video%d\n", dev->vdev.minor);
  1198. return err;
  1199. }
  1200. /* USB Stuff */
  1201. static int stk_camera_probe(struct usb_interface *interface,
  1202. const struct usb_device_id *id)
  1203. {
  1204. int i;
  1205. int err;
  1206. struct stk_camera *dev = NULL;
  1207. struct usb_device *udev = interface_to_usbdev(interface);
  1208. struct usb_host_interface *iface_desc;
  1209. struct usb_endpoint_descriptor *endpoint;
  1210. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1211. if (dev == NULL) {
  1212. STK_ERROR("Out of memory !\n");
  1213. return -ENOMEM;
  1214. }
  1215. kref_init(&dev->kref);
  1216. spin_lock_init(&dev->spinlock);
  1217. init_waitqueue_head(&dev->wait_frame);
  1218. dev->udev = udev;
  1219. dev->interface = interface;
  1220. usb_get_intf(interface);
  1221. dev->vsettings.vflip = vflip;
  1222. dev->vsettings.hflip = hflip;
  1223. dev->n_sbufs = 0;
  1224. set_present(dev);
  1225. /* Set up the endpoint information
  1226. * use only the first isoc-in endpoint
  1227. * for the current alternate setting */
  1228. iface_desc = interface->cur_altsetting;
  1229. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1230. endpoint = &iface_desc->endpoint[i].desc;
  1231. if (!dev->isoc_ep
  1232. && ((endpoint->bEndpointAddress
  1233. & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
  1234. && ((endpoint->bmAttributes
  1235. & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)) {
  1236. /* we found an isoc in endpoint */
  1237. dev->isoc_ep = (endpoint->bEndpointAddress & 0xF);
  1238. break;
  1239. }
  1240. }
  1241. if (!dev->isoc_ep) {
  1242. STK_ERROR("Could not find isoc-in endpoint");
  1243. kref_put(&dev->kref, stk_camera_cleanup);
  1244. return -ENODEV;
  1245. }
  1246. dev->vsettings.brightness = 0x7fff;
  1247. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1248. dev->vsettings.mode = MODE_VGA;
  1249. dev->frame_size = 640 * 480 * 2;
  1250. INIT_LIST_HEAD(&dev->sio_avail);
  1251. INIT_LIST_HEAD(&dev->sio_full);
  1252. usb_set_intfdata(interface, dev);
  1253. err = stk_register_video_device(dev);
  1254. if (err) {
  1255. kref_put(&dev->kref, stk_camera_cleanup);
  1256. return err;
  1257. }
  1258. stk_create_sysfs_files(&dev->vdev);
  1259. usb_autopm_enable(dev->interface);
  1260. return 0;
  1261. }
  1262. static void stk_camera_disconnect(struct usb_interface *interface)
  1263. {
  1264. struct stk_camera *dev = usb_get_intfdata(interface);
  1265. usb_set_intfdata(interface, NULL);
  1266. unset_present(dev);
  1267. wake_up_interruptible(&dev->wait_frame);
  1268. stk_remove_sysfs_files(&dev->vdev);
  1269. kref_put(&dev->kref, stk_camera_cleanup);
  1270. }
  1271. #ifdef CONFIG_PM
  1272. int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1273. {
  1274. struct stk_camera *dev = usb_get_intfdata(intf);
  1275. if (is_streaming(dev)) {
  1276. stk_stop_stream(dev);
  1277. /* yes, this is ugly */
  1278. set_streaming(dev);
  1279. }
  1280. return 0;
  1281. }
  1282. int stk_camera_resume(struct usb_interface *intf)
  1283. {
  1284. struct stk_camera *dev = usb_get_intfdata(intf);
  1285. if (!is_initialised(dev))
  1286. return 0;
  1287. unset_initialised(dev);
  1288. stk_initialise(dev);
  1289. stk_setup_format(dev);
  1290. if (is_streaming(dev))
  1291. stk_start_stream(dev);
  1292. return 0;
  1293. }
  1294. #endif
  1295. static struct usb_driver stk_camera_driver = {
  1296. .name = "stkwebcam",
  1297. .probe = stk_camera_probe,
  1298. .disconnect = stk_camera_disconnect,
  1299. .id_table = stkwebcam_table,
  1300. #ifdef CONFIG_PM
  1301. .suspend = stk_camera_suspend,
  1302. .resume = stk_camera_resume,
  1303. #endif
  1304. };
  1305. static int __init stk_camera_init(void)
  1306. {
  1307. int result;
  1308. result = usb_register(&stk_camera_driver);
  1309. if (result)
  1310. STK_ERROR("usb_register failed ! Error number %d\n", result);
  1311. return result;
  1312. }
  1313. static void __exit stk_camera_exit(void)
  1314. {
  1315. usb_deregister(&stk_camera_driver);
  1316. }
  1317. module_init(stk_camera_init);
  1318. module_exit(stk_camera_exit);