stk-webcam.c 36 KB

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