stk-webcam.c 35 KB

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