stk-webcam.c 36 KB

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