stk-webcam.c 36 KB

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