stk-webcam.c 36 KB

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