konicawc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * konicawc.c - konica webcam driver
  3. *
  4. * Author: Simon Evans <spse@secret.org.uk>
  5. *
  6. * Copyright (C) 2002 Simon Evans
  7. *
  8. * Licence: GPL
  9. *
  10. * Driver for USB webcams based on Konica chipset. This
  11. * chipset is used in Intel YC76 camera.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/input.h>
  18. #include <linux/usb_input.h>
  19. #include "usbvideo.h"
  20. #define MAX_BRIGHTNESS 108
  21. #define MAX_CONTRAST 108
  22. #define MAX_SATURATION 108
  23. #define MAX_SHARPNESS 108
  24. #define MAX_WHITEBAL 372
  25. #define MAX_SPEED 6
  26. #define MAX_CAMERAS 1
  27. #define DRIVER_VERSION "v1.4"
  28. #define DRIVER_DESC "Konica Webcam driver"
  29. enum ctrl_req {
  30. SetWhitebal = 0x01,
  31. SetBrightness = 0x02,
  32. SetSharpness = 0x03,
  33. SetContrast = 0x04,
  34. SetSaturation = 0x05,
  35. };
  36. enum frame_sizes {
  37. SIZE_160X120 = 0,
  38. SIZE_160X136 = 1,
  39. SIZE_176X144 = 2,
  40. SIZE_320X240 = 3,
  41. };
  42. #define MAX_FRAME_SIZE SIZE_320X240
  43. static struct usbvideo *cams;
  44. #ifdef CONFIG_USB_DEBUG
  45. static int debug;
  46. #define DEBUG(n, format, arg...) \
  47. if (n <= debug) { \
  48. printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
  49. }
  50. #else
  51. #define DEBUG(n, arg...)
  52. static const int debug = 0;
  53. #endif
  54. /* Some default values for initial camera settings,
  55. can be set by modprobe */
  56. static int size;
  57. static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
  58. static int brightness = MAX_BRIGHTNESS/2;
  59. static int contrast = MAX_CONTRAST/2;
  60. static int saturation = MAX_SATURATION/2;
  61. static int sharpness = MAX_SHARPNESS/2;
  62. static int whitebal = 3*(MAX_WHITEBAL/4);
  63. static int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
  64. /* These FPS speeds are from the windows config box. They are
  65. * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  66. * real fps.
  67. */
  68. static int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
  69. { 24, 40, 48, 60, 72, 80, 100 },
  70. { 18, 30, 36, 45, 54, 60, 75 },
  71. { 6, 10, 12, 15, 18, 21, 25 } };
  72. struct cam_size {
  73. u16 width;
  74. u16 height;
  75. u8 cmd;
  76. };
  77. static struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
  78. { 160, 136, 0xa },
  79. { 176, 144, 0x4 },
  80. { 320, 240, 0x5 } };
  81. struct konicawc {
  82. u8 brightness; /* camera uses 0 - 9, x11 for real value */
  83. u8 contrast; /* as above */
  84. u8 saturation; /* as above */
  85. u8 sharpness; /* as above */
  86. u8 white_bal; /* 0 - 33, x11 for real value */
  87. u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
  88. u8 size; /* Frame Size */
  89. int height;
  90. int width;
  91. struct urb *sts_urb[USBVIDEO_NUMSBUF];
  92. u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
  93. struct urb *last_data_urb;
  94. int lastframe;
  95. int cur_frame_size; /* number of bytes in current frame size */
  96. int maxline; /* number of lines per frame */
  97. int yplanesz; /* Number of bytes in the Y plane */
  98. unsigned int buttonsts:1;
  99. #ifdef CONFIG_INPUT
  100. struct input_dev input;
  101. char input_physname[64];
  102. #endif
  103. };
  104. #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
  105. #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
  106. #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
  107. static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  108. {
  109. int retval = usb_control_msg(uvd->dev,
  110. dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
  111. request, 0x40 | dir, value, index, buf, len, 1000);
  112. return retval < 0 ? retval : 0;
  113. }
  114. static inline void konicawc_camera_on(struct uvd *uvd)
  115. {
  116. DEBUG(0, "camera on");
  117. konicawc_set_misc(uvd, 0x2, 1, 0x0b);
  118. }
  119. static inline void konicawc_camera_off(struct uvd *uvd)
  120. {
  121. DEBUG(0, "camera off");
  122. konicawc_set_misc(uvd, 0x2, 0, 0x0b);
  123. }
  124. static void konicawc_set_camera_size(struct uvd *uvd)
  125. {
  126. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  127. konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
  128. cam->width = camera_sizes[cam->size].width;
  129. cam->height = camera_sizes[cam->size].height;
  130. cam->yplanesz = cam->height * cam->width;
  131. cam->cur_frame_size = (cam->yplanesz * 3) / 2;
  132. cam->maxline = cam->yplanesz / 256;
  133. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  134. }
  135. static int konicawc_setup_on_open(struct uvd *uvd)
  136. {
  137. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  138. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  139. cam->brightness * 11);
  140. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  141. DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
  142. cam->white_bal * 11);
  143. konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
  144. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  145. cam->contrast * 11);
  146. konicawc_set_value(uvd, cam->contrast, SetContrast);
  147. DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
  148. cam->saturation * 11);
  149. konicawc_set_value(uvd, cam->saturation, SetSaturation);
  150. DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
  151. cam->sharpness * 11);
  152. konicawc_set_value(uvd, cam->sharpness, SetSharpness);
  153. konicawc_set_camera_size(uvd);
  154. cam->lastframe = -2;
  155. cam->buttonsts = 0;
  156. return 0;
  157. }
  158. static void konicawc_adjust_picture(struct uvd *uvd)
  159. {
  160. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  161. konicawc_camera_off(uvd);
  162. DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
  163. uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
  164. if(cam->brightness != uvd->vpic.brightness / 11) {
  165. cam->brightness = uvd->vpic.brightness / 11;
  166. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  167. cam->brightness * 11);
  168. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  169. }
  170. DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
  171. uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
  172. if(cam->contrast != uvd->vpic.contrast / 11) {
  173. cam->contrast = uvd->vpic.contrast / 11;
  174. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  175. cam->contrast * 11);
  176. konicawc_set_value(uvd, cam->contrast, SetContrast);
  177. }
  178. konicawc_camera_on(uvd);
  179. }
  180. static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
  181. {
  182. char *cdata;
  183. int i, totlen = 0;
  184. unsigned char *status = stsurb->transfer_buffer;
  185. int keep = 0, discard = 0, bad = 0;
  186. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  187. for (i = 0; i < dataurb->number_of_packets; i++) {
  188. int button = cam->buttonsts;
  189. unsigned char sts;
  190. int n = dataurb->iso_frame_desc[i].actual_length;
  191. int st = dataurb->iso_frame_desc[i].status;
  192. cdata = dataurb->transfer_buffer +
  193. dataurb->iso_frame_desc[i].offset;
  194. /* Detect and ignore errored packets */
  195. if (st < 0) {
  196. DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
  197. i, n, st);
  198. uvd->stats.iso_err_count++;
  199. continue;
  200. }
  201. /* Detect and ignore empty packets */
  202. if (n <= 0) {
  203. uvd->stats.iso_skip_count++;
  204. continue;
  205. }
  206. /* See what the status data said about the packet */
  207. sts = *(status+stsurb->iso_frame_desc[i].offset);
  208. /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
  209. * otherwise:
  210. * bit 0 0: keep packet
  211. * 1: drop packet (padding data)
  212. *
  213. * bit 4 0 button not clicked
  214. * 1 button clicked
  215. * button is used to `take a picture' (in software)
  216. */
  217. if(sts < 0x80) {
  218. button = !!(sts & 0x40);
  219. sts &= ~0x40;
  220. }
  221. /* work out the button status, but don't do
  222. anything with it for now */
  223. if(button != cam->buttonsts) {
  224. DEBUG(2, "button: %sclicked", button ? "" : "un");
  225. cam->buttonsts = button;
  226. #ifdef CONFIG_INPUT
  227. input_report_key(&cam->input, BTN_0, cam->buttonsts);
  228. input_sync(&cam->input);
  229. #endif
  230. }
  231. if(sts == 0x01) { /* drop frame */
  232. discard++;
  233. continue;
  234. }
  235. if((sts > 0x01) && (sts < 0x80)) {
  236. info("unknown status %2.2x", sts);
  237. bad++;
  238. continue;
  239. }
  240. if(!sts && cam->lastframe == -2) {
  241. DEBUG(2, "dropping frame looking for image start");
  242. continue;
  243. }
  244. keep++;
  245. if(sts & 0x80) { /* frame start */
  246. unsigned char marker[] = { 0, 0xff, 0, 0x00 };
  247. if(cam->lastframe == -2) {
  248. DEBUG(2, "found initial image");
  249. cam->lastframe = -1;
  250. }
  251. marker[3] = sts & 0x7F;
  252. RingQueue_Enqueue(&uvd->dp, marker, 4);
  253. totlen += 4;
  254. }
  255. totlen += n; /* Little local accounting */
  256. RingQueue_Enqueue(&uvd->dp, cdata, n);
  257. }
  258. DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
  259. keep, discard, bad, totlen);
  260. return totlen;
  261. }
  262. static void resubmit_urb(struct uvd *uvd, struct urb *urb)
  263. {
  264. int i, ret;
  265. for (i = 0; i < FRAMES_PER_DESC; i++) {
  266. urb->iso_frame_desc[i].status = 0;
  267. }
  268. urb->dev = uvd->dev;
  269. urb->status = 0;
  270. ret = usb_submit_urb(urb, GFP_ATOMIC);
  271. DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
  272. if(ret)
  273. err("usb_submit_urb error (%d)", ret);
  274. }
  275. static void konicawc_isoc_irq(struct urb *urb, struct pt_regs *regs)
  276. {
  277. struct uvd *uvd = urb->context;
  278. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  279. /* We don't want to do anything if we are about to be removed! */
  280. if (!CAMERA_IS_OPERATIONAL(uvd))
  281. return;
  282. if (!uvd->streaming) {
  283. DEBUG(1, "Not streaming, but interrupt!");
  284. return;
  285. }
  286. DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
  287. uvd->stats.urb_count++;
  288. if (urb->transfer_buffer_length > 32) {
  289. cam->last_data_urb = urb;
  290. return;
  291. }
  292. /* Copy the data received into ring queue */
  293. if(cam->last_data_urb) {
  294. int len = 0;
  295. if(urb->start_frame != cam->last_data_urb->start_frame)
  296. err("Lost sync on frames");
  297. else if (!urb->status && !cam->last_data_urb->status)
  298. len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
  299. resubmit_urb(uvd, cam->last_data_urb);
  300. resubmit_urb(uvd, urb);
  301. cam->last_data_urb = NULL;
  302. uvd->stats.urb_length = len;
  303. uvd->stats.data_count += len;
  304. if(len)
  305. RingQueue_WakeUpInterruptible(&uvd->dp);
  306. return;
  307. }
  308. return;
  309. }
  310. static int konicawc_start_data(struct uvd *uvd)
  311. {
  312. struct usb_device *dev = uvd->dev;
  313. int i, errFlag;
  314. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  315. int pktsz;
  316. struct usb_interface *intf;
  317. struct usb_host_interface *interface = NULL;
  318. intf = usb_ifnum_to_if(dev, uvd->iface);
  319. if (intf)
  320. interface = usb_altnum_to_altsetting(intf,
  321. spd_to_iface[cam->speed]);
  322. if (!interface)
  323. return -ENXIO;
  324. pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
  325. DEBUG(1, "pktsz = %d", pktsz);
  326. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  327. err("Camera is not operational");
  328. return -EFAULT;
  329. }
  330. uvd->curframe = -1;
  331. konicawc_camera_on(uvd);
  332. /* Alternate interface 1 is is the biggest frame size */
  333. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  334. if (i < 0) {
  335. err("usb_set_interface error");
  336. uvd->last_error = i;
  337. return -EBUSY;
  338. }
  339. /* We double buffer the Iso lists */
  340. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  341. int j, k;
  342. struct urb *urb = uvd->sbuf[i].urb;
  343. urb->dev = dev;
  344. urb->context = uvd;
  345. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  346. urb->interval = 1;
  347. urb->transfer_flags = URB_ISO_ASAP;
  348. urb->transfer_buffer = uvd->sbuf[i].data;
  349. urb->complete = konicawc_isoc_irq;
  350. urb->number_of_packets = FRAMES_PER_DESC;
  351. urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
  352. for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
  353. urb->iso_frame_desc[j].offset = k;
  354. urb->iso_frame_desc[j].length = pktsz;
  355. }
  356. urb = cam->sts_urb[i];
  357. urb->dev = dev;
  358. urb->context = uvd;
  359. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
  360. urb->interval = 1;
  361. urb->transfer_flags = URB_ISO_ASAP;
  362. urb->transfer_buffer = cam->sts_buf[i];
  363. urb->complete = konicawc_isoc_irq;
  364. urb->number_of_packets = FRAMES_PER_DESC;
  365. urb->transfer_buffer_length = FRAMES_PER_DESC;
  366. for (j=0; j < FRAMES_PER_DESC; j++) {
  367. urb->iso_frame_desc[j].offset = j;
  368. urb->iso_frame_desc[j].length = 1;
  369. }
  370. }
  371. cam->last_data_urb = NULL;
  372. /* Submit all URBs */
  373. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  374. errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
  375. if (errFlag)
  376. err("usb_submit_isoc(%d) ret %d", i, errFlag);
  377. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  378. if (errFlag)
  379. err ("usb_submit_isoc(%d) ret %d", i, errFlag);
  380. }
  381. uvd->streaming = 1;
  382. DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
  383. return 0;
  384. }
  385. static void konicawc_stop_data(struct uvd *uvd)
  386. {
  387. int i, j;
  388. struct konicawc *cam;
  389. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  390. return;
  391. konicawc_camera_off(uvd);
  392. uvd->streaming = 0;
  393. cam = (struct konicawc *)uvd->user_data;
  394. cam->last_data_urb = NULL;
  395. /* Unschedule all of the iso td's */
  396. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  397. usb_kill_urb(uvd->sbuf[i].urb);
  398. usb_kill_urb(cam->sts_urb[i]);
  399. }
  400. if (!uvd->remove_pending) {
  401. /* Set packet size to 0 */
  402. j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
  403. if (j < 0) {
  404. err("usb_set_interface() error %d.", j);
  405. uvd->last_error = j;
  406. }
  407. }
  408. }
  409. static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
  410. {
  411. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  412. int maxline = cam->maxline;
  413. int yplanesz = cam->yplanesz;
  414. assert(frame != NULL);
  415. DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
  416. DEBUG(3, "Frame state = %d", frame->scanstate);
  417. if(frame->scanstate == ScanState_Scanning) {
  418. int drop = 0;
  419. int curframe;
  420. int fdrops = 0;
  421. DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
  422. while(RingQueue_GetLength(&uvd->dp) >= 4) {
  423. if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
  424. (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
  425. (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
  426. (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
  427. curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
  428. if(cam->lastframe >= 0) {
  429. fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
  430. fdrops--;
  431. if(fdrops) {
  432. info("Dropped %d frames (%d -> %d)", fdrops,
  433. cam->lastframe, curframe);
  434. }
  435. }
  436. cam->lastframe = curframe;
  437. frame->curline = 0;
  438. frame->scanstate = ScanState_Lines;
  439. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
  440. break;
  441. }
  442. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
  443. drop++;
  444. }
  445. if(drop)
  446. DEBUG(2, "dropped %d bytes looking for new frame", drop);
  447. }
  448. if(frame->scanstate == ScanState_Scanning)
  449. return;
  450. /* Try to move data from queue into frame buffer
  451. * We get data in blocks of 384 bytes made up of:
  452. * 256 Y, 64 U, 64 V.
  453. * This needs to be written out as a Y plane, a U plane and a V plane.
  454. */
  455. while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
  456. /* Y */
  457. RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
  458. /* U */
  459. RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
  460. /* V */
  461. RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
  462. frame->seqRead_Length += 384;
  463. frame->curline++;
  464. }
  465. /* See if we filled the frame */
  466. if (frame->curline == maxline) {
  467. DEBUG(5, "got whole frame");
  468. frame->frameState = FrameState_Done_Hold;
  469. frame->curline = 0;
  470. uvd->curframe = -1;
  471. uvd->stats.frame_num++;
  472. }
  473. }
  474. static int konicawc_find_fps(int size, int fps)
  475. {
  476. int i;
  477. fps *= 3;
  478. DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
  479. if(fps <= spd_to_fps[size][0])
  480. return 0;
  481. if(fps >= spd_to_fps[size][MAX_SPEED])
  482. return MAX_SPEED;
  483. for(i = 0; i < MAX_SPEED; i++) {
  484. if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
  485. DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
  486. if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
  487. return i;
  488. else
  489. return i+1;
  490. }
  491. }
  492. return MAX_SPEED+1;
  493. }
  494. static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
  495. {
  496. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  497. int newspeed = cam->speed;
  498. int newsize;
  499. int x = vw->width;
  500. int y = vw->height;
  501. int fps = vw->flags;
  502. if(x > 0 && y > 0) {
  503. DEBUG(2, "trying to find size %d,%d", x, y);
  504. for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
  505. if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
  506. break;
  507. }
  508. } else {
  509. newsize = cam->size;
  510. }
  511. if(newsize > MAX_FRAME_SIZE) {
  512. DEBUG(1, "couldn't find size %d,%d", x, y);
  513. return -EINVAL;
  514. }
  515. if(fps > 0) {
  516. DEBUG(1, "trying to set fps to %d", fps);
  517. newspeed = konicawc_find_fps(newsize, fps);
  518. DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
  519. }
  520. if(newspeed > MAX_SPEED)
  521. return -EINVAL;
  522. DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
  523. if((newsize == cam->size) && (newspeed == cam->speed)) {
  524. DEBUG(1, "Nothing to do");
  525. return 0;
  526. }
  527. DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
  528. camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
  529. konicawc_stop_data(uvd);
  530. uvd->ifaceAltActive = spd_to_iface[newspeed];
  531. DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
  532. cam->speed = newspeed;
  533. if(cam->size != newsize) {
  534. cam->size = newsize;
  535. konicawc_set_camera_size(uvd);
  536. }
  537. /* Flush the input queue and clear any current frame in progress */
  538. RingQueue_Flush(&uvd->dp);
  539. cam->lastframe = -2;
  540. if(uvd->curframe != -1) {
  541. uvd->frame[uvd->curframe].curline = 0;
  542. uvd->frame[uvd->curframe].seqRead_Length = 0;
  543. uvd->frame[uvd->curframe].seqRead_Index = 0;
  544. }
  545. konicawc_start_data(uvd);
  546. return 0;
  547. }
  548. static int konicawc_calculate_fps(struct uvd *uvd)
  549. {
  550. struct konicawc *cam = uvd->user_data;
  551. return spd_to_fps[cam->size][cam->speed]/3;
  552. }
  553. static void konicawc_configure_video(struct uvd *uvd)
  554. {
  555. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  556. u8 buf[2];
  557. memset(&uvd->vpic, 0, sizeof(uvd->vpic));
  558. memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
  559. RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
  560. RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
  561. RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
  562. RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
  563. RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
  564. cam->brightness = brightness / 11;
  565. cam->contrast = contrast / 11;
  566. cam->saturation = saturation / 11;
  567. cam->sharpness = sharpness / 11;
  568. cam->white_bal = whitebal / 11;
  569. uvd->vpic.colour = 108;
  570. uvd->vpic.hue = 108;
  571. uvd->vpic.brightness = brightness;
  572. uvd->vpic.contrast = contrast;
  573. uvd->vpic.whiteness = whitebal;
  574. uvd->vpic.depth = 6;
  575. uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
  576. memset(&uvd->vcap, 0, sizeof(uvd->vcap));
  577. strcpy(uvd->vcap.name, "Konica Webcam");
  578. uvd->vcap.type = VID_TYPE_CAPTURE;
  579. uvd->vcap.channels = 1;
  580. uvd->vcap.audios = 0;
  581. uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
  582. uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
  583. uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
  584. uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
  585. memset(&uvd->vchan, 0, sizeof(uvd->vchan));
  586. uvd->vchan.flags = 0 ;
  587. uvd->vchan.tuners = 0;
  588. uvd->vchan.channel = 0;
  589. uvd->vchan.type = VIDEO_TYPE_CAMERA;
  590. strcpy(uvd->vchan.name, "Camera");
  591. /* Talk to device */
  592. DEBUG(1, "device init");
  593. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  594. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  595. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  596. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  597. if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
  598. DEBUG(2, "2,0,d failed");
  599. DEBUG(1, "setting initial values");
  600. }
  601. static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  602. {
  603. struct usb_device *dev = interface_to_usbdev(intf);
  604. struct uvd *uvd = NULL;
  605. int ix, i, nas;
  606. int actInterface=-1, inactInterface=-1, maxPS=0;
  607. unsigned char video_ep = 0;
  608. DEBUG(1, "konicawc_probe(%p)", intf);
  609. /* We don't handle multi-config cameras */
  610. if (dev->descriptor.bNumConfigurations != 1)
  611. return -ENODEV;
  612. info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
  613. RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
  614. /* Validate found interface: must have one ISO endpoint */
  615. nas = intf->num_altsetting;
  616. if (nas != 8) {
  617. err("Incorrect number of alternate settings (%d) for this camera!", nas);
  618. return -ENODEV;
  619. }
  620. /* Validate all alternate settings */
  621. for (ix=0; ix < nas; ix++) {
  622. const struct usb_host_interface *interface;
  623. const struct usb_endpoint_descriptor *endpoint;
  624. interface = &intf->altsetting[ix];
  625. i = interface->desc.bAlternateSetting;
  626. if (interface->desc.bNumEndpoints != 2) {
  627. err("Interface %d. has %u. endpoints!",
  628. interface->desc.bInterfaceNumber,
  629. (unsigned)(interface->desc.bNumEndpoints));
  630. return -ENODEV;
  631. }
  632. endpoint = &interface->endpoint[1].desc;
  633. DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
  634. endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
  635. if (video_ep == 0)
  636. video_ep = endpoint->bEndpointAddress;
  637. else if (video_ep != endpoint->bEndpointAddress) {
  638. err("Alternate settings have different endpoint addresses!");
  639. return -ENODEV;
  640. }
  641. if ((endpoint->bmAttributes & 0x03) != 0x01) {
  642. err("Interface %d. has non-ISO endpoint!",
  643. interface->desc.bInterfaceNumber);
  644. return -ENODEV;
  645. }
  646. if ((endpoint->bEndpointAddress & 0x80) == 0) {
  647. err("Interface %d. has ISO OUT endpoint!",
  648. interface->desc.bInterfaceNumber);
  649. return -ENODEV;
  650. }
  651. if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
  652. if (inactInterface < 0)
  653. inactInterface = i;
  654. else {
  655. err("More than one inactive alt. setting!");
  656. return -ENODEV;
  657. }
  658. } else {
  659. if (i == spd_to_iface[speed]) {
  660. /* This one is the requested one */
  661. actInterface = i;
  662. }
  663. }
  664. if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
  665. maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
  666. }
  667. if(actInterface == -1) {
  668. err("Cant find required endpoint");
  669. return -ENODEV;
  670. }
  671. DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
  672. uvd = usbvideo_AllocateDevice(cams);
  673. if (uvd != NULL) {
  674. struct konicawc *cam = (struct konicawc *)(uvd->user_data);
  675. /* Here uvd is a fully allocated uvd object */
  676. for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
  677. cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  678. if(cam->sts_urb[i] == NULL) {
  679. while(i--) {
  680. usb_free_urb(cam->sts_urb[i]);
  681. }
  682. err("can't allocate urbs");
  683. return -ENOMEM;
  684. }
  685. }
  686. cam->speed = speed;
  687. RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
  688. cam->width = camera_sizes[size].width;
  689. cam->height = camera_sizes[size].height;
  690. cam->size = size;
  691. uvd->flags = 0;
  692. uvd->debug = debug;
  693. uvd->dev = dev;
  694. uvd->iface = intf->altsetting->desc.bInterfaceNumber;
  695. uvd->ifaceAltInactive = inactInterface;
  696. uvd->ifaceAltActive = actInterface;
  697. uvd->video_endp = video_ep;
  698. uvd->iso_packet_len = maxPS;
  699. uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
  700. uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
  701. uvd->canvas = VIDEOSIZE(320, 240);
  702. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  703. /* Initialize konicawc specific data */
  704. konicawc_configure_video(uvd);
  705. i = usbvideo_RegisterVideoDevice(uvd);
  706. uvd->max_frame_size = (320 * 240 * 3)/2;
  707. if (i != 0) {
  708. err("usbvideo_RegisterVideoDevice() failed.");
  709. uvd = NULL;
  710. }
  711. #ifdef CONFIG_INPUT
  712. /* Register input device for button */
  713. memset(&cam->input, 0, sizeof(struct input_dev));
  714. cam->input.name = "Konicawc snapshot button";
  715. cam->input.private = cam;
  716. cam->input.evbit[0] = BIT(EV_KEY);
  717. cam->input.keybit[LONG(BTN_0)] = BIT(BTN_0);
  718. usb_to_input_id(dev, &cam->input.id);
  719. input_register_device(&cam->input);
  720. usb_make_path(dev, cam->input_physname, 56);
  721. strcat(cam->input_physname, "/input0");
  722. cam->input.phys = cam->input_physname;
  723. info("konicawc: %s on %s\n", cam->input.name, cam->input.phys);
  724. #endif
  725. }
  726. if (uvd) {
  727. usb_set_intfdata (intf, uvd);
  728. return 0;
  729. }
  730. return -EIO;
  731. }
  732. static void konicawc_free_uvd(struct uvd *uvd)
  733. {
  734. int i;
  735. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  736. #ifdef CONFIG_INPUT
  737. input_unregister_device(&cam->input);
  738. #endif
  739. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  740. usb_free_urb(cam->sts_urb[i]);
  741. cam->sts_urb[i] = NULL;
  742. }
  743. }
  744. static struct usb_device_id id_table[] = {
  745. { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
  746. { } /* Terminating entry */
  747. };
  748. static int __init konicawc_init(void)
  749. {
  750. struct usbvideo_cb cbTbl;
  751. info(DRIVER_DESC " " DRIVER_VERSION);
  752. memset(&cbTbl, 0, sizeof(cbTbl));
  753. cbTbl.probe = konicawc_probe;
  754. cbTbl.setupOnOpen = konicawc_setup_on_open;
  755. cbTbl.processData = konicawc_process_isoc;
  756. cbTbl.getFPS = konicawc_calculate_fps;
  757. cbTbl.setVideoMode = konicawc_set_video_mode;
  758. cbTbl.startDataPump = konicawc_start_data;
  759. cbTbl.stopDataPump = konicawc_stop_data;
  760. cbTbl.adjustPicture = konicawc_adjust_picture;
  761. cbTbl.userFree = konicawc_free_uvd;
  762. return usbvideo_register(
  763. &cams,
  764. MAX_CAMERAS,
  765. sizeof(struct konicawc),
  766. "konicawc",
  767. &cbTbl,
  768. THIS_MODULE,
  769. id_table);
  770. }
  771. static void __exit konicawc_cleanup(void)
  772. {
  773. usbvideo_Deregister(&cams);
  774. }
  775. MODULE_DEVICE_TABLE(usb, id_table);
  776. MODULE_LICENSE("GPL");
  777. MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
  778. MODULE_DESCRIPTION(DRIVER_DESC);
  779. module_param(speed, int, 0);
  780. MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
  781. module_param(size, int, 0);
  782. MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
  783. module_param(brightness, int, 0);
  784. MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
  785. module_param(contrast, int, 0);
  786. MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
  787. module_param(saturation, int, 0);
  788. MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
  789. module_param(sharpness, int, 0);
  790. MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
  791. module_param(whitebal, int, 0);
  792. MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
  793. #ifdef CONFIG_USB_DEBUG
  794. module_param(debug, int, S_IRUGO | S_IWUSR);
  795. MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
  796. #endif
  797. module_init(konicawc_init);
  798. module_exit(konicawc_cleanup);