stk-webcam.c 36 KB

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