Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * File Name : hrt.c
  3  * 
  4  * This is a device driver for the High Resolution Technologies
  5  * Pixelsmart 512-8 frame grabber.
  6 
  7  *         Copyright (C) 2003, Florida State University
  8  *
  9  * This is free software; you can redistribute it and/or modify it under 
 10  * terms of the GNU General Public License as published  by the Free Soft- 
 11  * ware Foundation, Version 2.  This software is distributed in the hope
 12  * that it will be useful, but WITH OUT ANY WARRANTY;  without even the
 13  * implied warranty of MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 14  * See the GNU General Public License for  more details. You should have
 15  * received  a copy of the GNU General Public License distributed with this
 16  * software; see file COPYING.  If not, write to  the Free Software Foundation,
 17  * 59 Temple Place - Suite 330,  Boston, MA 02111-1307, USA.
 18 
 19  * This is based on two drivers, originally written by two
 20  * teams of students:
 21  * 1) Brett W. Thompson, Gilberto Morejon, and Alex Rudnick
 22  * 2) Veena Adityan and Arthi Gokarn
 23  * It was the final project for the course CIS 5930 Linux Kernel &
 24  * Device Driver Programming, at the Florida State University,
 25  * Summer 2003.
 26 
 27  * I2C initialization and probing were written by Dr. T. P. Baker
 28  * and adapted for i2c-core by Brett W. Thompson.
 29 
 30  * Merging the two drivers was started by Dr. T. P. Baker and
 31  * finished by Brett W. Thompson and Gilberto Morejon.
 32 
 33  * Streaming portion adapted from Bill Dirks' v4l2cap.c by
 34  * Veena Adityan and Arthi Gokarn.
 35 
 36  */
 37 
 38 #include <linux/module.h>
 39 #include <linux/pci.h>
 40 #include <linux/init.h>
 41 #include <linux/videodev.h>
 42 #include <linux/list.h>
 43 
 44 #include <linux/delay.h>
 45 #include <asm/msr.h>
 46 #include <linux/i2c.h>
 47 //#include <linux/i2c-algo-bit.h>
 48 
 49 #include "hrt.h"
 50 
 51 #ifndef HAVE_V4L2
 52 #  error Sorry, this module requires v4l2. You should patch your kernel with v4l2 or you could download the non-v4l2 dependent module hrtmem.c.
 53 #endif
 54 
 55 /* Workaround; put our conditional here so we don't have to
 56    for every other usage of these */
 57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
 58 #  define __DEVEXIT
 59 #  define __DEVINIT
 60 #  define __DEVEXIT_P
 61 #else
 62 #  define __DEVEXIT __devexit
 63 #  define __DEVINIT __devinit
 64 #  define __DEVEXIT_P __devexit_p
 65 #endif
 66 
 67 /**
 68  * hrt_ctls - controls the user can set via V4L ioctl's 
 69  */
 70 static const struct v4l2_queryctrl hrt_ctls[] = {
 71         {
 72                 .id = V4L2_CID_BRIGHTNESS,
 73                 .name = "Brightness",
 74                 .minimum = 0,
 75                 .maximum = 255,
 76                 .step = 1,
 77                 .default_value = 0x9b,
 78                 .type = V4L2_CTRL_TYPE_INTEGER,
 79         }, {
 80                 .id = V4L2_CID_CONTRAST,
 81                 .name = "Contrast",
 82                 .minimum = 0,
 83                 .maximum = 255,
 84                 .step = 1,
 85                 .default_value = 0x5e,
 86                 .type = V4L2_CTRL_TYPE_INTEGER,
 87         }
 88 };
 89 
 90 /**
 91  * users - global number of references to the device. When
 92  *  it reaches 0, we can turn off the timer.
 93  */
 94 static unsigned int users = 0;
 95 
 96 /**
 97  * hrt_addresses - a list of possible jumper-selected addresses
 98  *  jumper A on  = plug and play address (above one MB address)
 99  *  jumper A off = hardwired to 0xdc000 or 0xd4000
100  *  jumper B on  = address 0xd4000 (ignored if jumper A is on) 
101  *  jumper B off = address 0xdc000 (ignored if jumper A is on) 
102  */
103 const unsigned long hrt_addresses[] = { 0xd4000, 0xdc000 };
104 
105 /**
106  * I2C routines -
107  *  Consider providing this capability also for an application
108  *  that is using raw memory-mapping, maybe as a separate header
109  *  file and object library
110  */
111 
112 /**
113  * saa7110_default_init_regs - the register values used to
114  * initialize the SAA7110 A/D converter.
115  * Because some registers are not set, this is given
116  * as a list of pairs.  The first element of each pair
117  * is the register number, and the second number is the
118  * value of the register.  The array is terminated by
119  * a double zero-byte.  This generalization allows us to reuse
120  * the initialization routine with different tables,
121  * to allow an application to reset any set of device
122  * registers.
123  */
124 const unsigned char saa7110_default_init_regs[] = {
125         94,                     /* there are 94 bytes that follow */
126         0x00, 0x4c,             /* increment delay (IDEL) */
127         0x01, 0x3c,             /* HSY begin 50 Hz */
128         0x02, 0x0d,             /* HSY stop 50 Hz */
129         0x03, 0xef,             /* HCL begin 50 Hz */
130         0x04, 0xbd,             /* HCL stop 50 Hz */
131         0x05, 0xf0,             /* HSY after PHI1 50 Hz */
132         0x06, 0x00,             /* luminance control */
133         0x07, 0x00,             /* hue control */
134         0x08, 0xf8,             /* colour killer threshold QUAM (PAL/NTSC) */
135         0x09, 0xf8,             /* colour killer threshold SECAM */
136         0x0A, 0x60,             /* PAL switch sensitivity */
137         0x0B, 0x50,             /* SECAM switch sensitivity */
138         0x0C, 0x00,             /* gain control chrominance */
139         0x0D, 0x86,             /* standard/mode control */
140         /* 7 VTRC = 1  (VCR mode, not TV)
141            6 XXX
142            5 XXX
143            4 XXX
144            3 RTSE = 0  (PLIN switched to output)
145            2 HRMV = 1  (HREF normal position)
146            1 SSTB = 1  (status byte = 1)
147            0 SECS = 0  (other standards, not SECAM) */
148         0x0E, 0x18,             /* I/O and clock control */
149         0x0F, 0x90,             /* control #1 */
150         0x10, 0x00,             /* control #2 */
151         0x11, 0x2c,             /* chrominance gain reference */
152         0x12, 0x7f,             /* chrominance saturation */
153         0x13, 0x5e,             /* luminance contrast */
154         0x14, 0x42,             /* HSY begin 60 Hz */
155         0x15, 0x1a,             /* HSY stop 60 Hz */
156         0x16, 0xff,             /* HCL begin 60 Hz */
157         0x17, 0xda,             /* HCL stop 60 Hz */
158         0x18, 0xf0,             /* HSY after PHI1 60 Hz */
159         0x19, 0x9b,             /* luminance brightness */
160         /*
161            0x1A - not used
162            0x1B - not used
163            0x1C - not used
164            0x1D - not used
165            0x1E - not used
166            0x1F - not used
167          */
168         0x20, 0x7c,             /* analog control #1 */
169         0x21, 0x03,             /* analog control #2 */
170         0x22, 0xd2,             /* mixer control #1 */
171         0x23, 0x41,             /* clamping level control 21 */
172         0x24, 0x80,             /* clamping level control 22 */
173         0x25, 0x41,             /* clamping level control 31 */
174         0x26, 0x80,             /* clamping level control 32 */
175         0x27, 0x4f,             /* gain control #1 */
176         0x28, 0xfe,             /* white peak control */
177         0x29, 0x01,             /* sync bottom control */
178         0x2A, 0xcf,             /* gain control analog #2 */
179         0x2B, 0x0f,             /* gain control analog #3 */
180         0x2C, 0x83,             /* mixer control #2 */
181         0x2D, 0x01,             /* integration value gain */
182         0x2E, 0x81,             /* vertical blanking pulse set */
183         0x2F, 0x03,             /* vertical blanking pulse reset */
184         0x30, 0x60,             /* ADCs gain control */
185         0x31, 0x71,             /* mixer control #3 */
186         0x32, 0x02,             /* integration value white peak */
187         0x33, 0x8c,             /* mixer control #4 */
188         0x34, 0x03,             /* gain update level */
189 };
190 
191 /* Number of registers on the board- checked in i2c_init() */
192 #define HRT_NUMREGS 0x34
193 
194 /* Small utility used in read()... Probably there's an official
195    one somewhere in the kernel, huh?
196 */
197 #ifndef min
198 #  define min(a,b) ((a)<(b) ? (a) : (b))
199 #endif
200 
201 /**
202  * struct stream_buffer - represents a single buffer for streaming
203  */
204 struct stream_buffer {
205         struct v4l2_buffer vidbuf; /* For the V4L ioctl's */
206         int fields_grabbed; /* Two-bit pair of flags indicating which fields
207                                have been grabbed (i.e., 00 = no fields,
208                                11 = both) */
209         struct list_head node; /* Connection into queues */
210         int requested; /* Set by REQBUF ioctl */
211         unsigned char *vaddress; /* This is the pointer to the vmalloc()'d
212                                     memory into which the actual frame goes */
213         int vma_refcount; /* Reference count */
214 };
215 
216 /**
217  * struct stream_buffer_queue - FIFO of stream_buffer structs
218  */
219 struct stream_buffer_queue {
220         struct stream_buffer *streambuf; /* Pointer to node data */
221         struct list_head head; /* Start of the queue */
222         rwlock_t lock; /* Lock to protect the queue */
223         int length; /* How many nodes are on the queue */
224 };
225 
226 /**
227  * struct hrt_per_file - contains data to be stored per file handle
228  */
229 struct hrt_per_file {
230         struct v4l2_rect win;
231         struct hrt *hrtdev;
232 };
233 
234 /**
235  * struct hrt - the main device struct. This represents the card.
236  */
237 struct hrt {
238         unsigned long physaddr; /* Physical address */
239         unsigned long virtaddr; /* Virtual address (was ioremapped) */
240         struct pci_dev *pcidev; /* PCI device */
241         struct semaphore sem; /* Protective mutex */
242         unsigned int field_bit; /* Which field (even or odd) is capturing */
243         wait_queue_head_t waitqueue; /* Wait queue for poll() */
244         unsigned char regvals[HRT_NUMREGS]; /* Current values of the regs */
245         unsigned char *framedata; /* The data for a frame for read() */
246         struct v4l2_rect *win; /* Current region of interest */
247         unsigned int bytesperline; /* Bytes per raster line */
248         unsigned int framesize; /* Size of a frame */
249         unsigned int num; /* Card number (for multiple cards) */
250 
251         struct i2c_adapter i2c_adap; /* I2C adapter */
252         struct i2c_algorithm i2c_algo; /* I2C algorithm */
253         struct i2c_client i2c_client; /* I2C client */
254 
255         struct video_device video_dev; /* For registering with V4L */
256         int streaming; /* Flag for streaming mode */
257         int numbufs; /* Number of buffers for streaming */
258         struct stream_buffer *streambufs; /* Array of buffers */
259 
260         struct stream_buffer_queue capture_list; /* List of queued buffers */
261         struct stream_buffer_queue done_list; /* List of filled buffers */
262 
263         /* Veena and Arthi's streaming */
264         struct v4l2_format clientfmt;
265         struct v4l2_captureparm capture;
266         int stream_buffers_mapped;
267         struct stream_buffer stream_buf[MAX_CAPTURE_BUFFERS];
268         int stream_buffers_requested;
269 };
270 
271 /* Multiple card support */
272 #define HRT_MAX_DEVS 3
273 static struct hrt hrtdevs[HRT_MAX_DEVS];
274 
275 /* Number of devices found/allocated */
276 static unsigned int num_hrtdevs = 0;
277 
278 /* Flag to tell whether the kernel timer is running */
279 volatile static int timer_running = 0;
280 
281 /* The kernel timer */
282 static struct timer_list timer;
283 
284 /* Minor number, or -1 for first free */
285 static int video_minor = -1;
286 
287 /* Don't put V4L2_CAP_STREAMING in capabilities; recommended for xawtv */
288 static unsigned int disable_streaming = 0;
289 
290 /* Only one user can open /dev/video<n> at at time */
291 static unsigned int exclusive = 0;
292 
293 /* Debugging output */
294 static unsigned int debug = 1;
295 
296 /* Flag to prevent call to pci_unregister_device() if
297    pci_module_init() found no devices (i.e., the jumpers are set
298    for a non-PCI address) */
299 static unsigned int no_pci = 0;
300 
301 MODULE_PARM(debug, "i");
302 MODULE_PARM_DESC(debug, "Enable debugging output (default: off)");
303 MODULE_PARM(exclusive, "i");
304 MODULE_PARM_DESC(exclusive,
305                  "Only one process can open the device (default: off)");
306 
307 MODULE_PARM(video_minor, "i");
308 MODULE_PARM_DESC(video_minor, "Device's minor number (default: dynamic)");
309 MODULE_PARM(disable_streaming, "i");
310 MODULE_PARM_DESC(disable_streaming, "Don't return V4L2_CAP_STREAMING; recommended for xawtv (default: off)");
311 
312 MODULE_AUTHOR("Brett W. Thompson, Gilberto Morejon, Veena Adityan, Arthi Gokarn, Alex Rudnick");
313 MODULE_DESCRIPTION("Driver for HRT Pixelsmart 512-8-PCI");
314 MODULE_LICENSE("GPL");
315 
316 /**
317  * dprintk, hrt_printk - thin wrappers around printk()
318  */
319 #define dprintk(fmt, arg...)    if (debug) \
320         printk("hrt: " fmt, ## arg)
321 #define hrt_printk(fmt, arg...) printk("hrt: " fmt, ## arg)
322 
323 /* Prototypes */
324 void inline grab_field(struct hrt *hrtdev, unsigned char *framedata,
325                        int parity);
326 static void timer_func(unsigned long ptr);
327 static int i2c_set_reg(struct hrt *hrtdev, int reg, unsigned char val);
328 struct hrt_per_file *per_file_init(struct hrt *hrtdev);
329 static int hrt_probe(unsigned long addr);
330 
331 static void __DEVEXIT hrt_remove(struct pci_dev *pci_dev);
332 static int __DEVINIT hrt_pci_probe(struct pci_dev *dev,
333                                    const struct pci_device_id *pci_id);
334 
335 /* Dr. Baker's I2C prototypes */
336 static int i2c_init(struct hrt *hrtdev,
337                     const unsigned char *init_regs);
338 static int hrt_i2c_send_byte(unsigned long addr, unsigned char data);
339 
340 /* Veena and Arthi's streaming functions */
341 int mmap_request_buffers(struct hrt *dev,
342                          struct v4l2_requestbuffers *req);
343 int hrt_streamon(struct hrt *hrtdev, __u32 type);
344 void hrt_streamoff(struct hrt *hrtdev, __u32 type);
345 int hrt_dequeuebuffer(struct hrt *hrtdev,
346                       struct v4l2_buffer *buf);
347 int hrt_queuebuffer(struct hrt *hrtdev,
348                     struct v4l2_buffer *buf);
349 
350 /* Prototypes for file_operations */
351 static int hrt_open(struct inode *inode, struct file *file);
352 static int hrt_release(struct inode *inode, struct file *file);
353 static int hrt_mmap(struct file *file, struct vm_area_struct *vma);
354 static int hrt_ioctl(struct inode *inode, struct file *file,
355                      unsigned int cmd, unsigned long arg);
356 static int hrt_do_private_ioctl(struct hrt *hrtdev, unsigned int cmd,
357                                 unsigned long arg);
358 static unsigned int hrt_poll(struct file *file, poll_table * wait);
359 static int hrt_read(struct file *file, char *buf, size_t count,
360                     loff_t * ppos);
361 
362 /**
363  * hrt_pci_tbl - table of values for pci_module_init()
364  */
365 static struct pci_device_id hrt_pci_tbl[] __devinitdata = {
366         {HRT_VENDOR_ID, HRT_DEVICE_ID_GRAY, PCI_ANY_ID, PCI_ANY_ID,
367           0, 0, 0},
368         {0,}
369 };
370 MODULE_DEVICE_TABLE(pci, hrt_pci_tbl);
371 
372 static struct pci_driver hrt_pci_driver = {
373         .name     = "hrt",
374         .id_table = hrt_pci_tbl,
375         .probe    = hrt_pci_probe,
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
377         .remove   = hrt_remove,
378 #else
379         .remove   = __DEVEXIT_P(hrt_remove),
380 #endif
381 };
382 
383 /**
384  * hrt_fops - fops for /dev/video<n>
385  */
386 static struct file_operations hrt_fops = {
387         .owner = THIS_MODULE,
388         .open = hrt_open,
389         .mmap = hrt_mmap,
390         .release = hrt_release,
391         .ioctl = hrt_ioctl,
392         .read = hrt_read,
393         .llseek = no_llseek,
394         .poll = hrt_poll,
395 };
396 
397 /* XXX: Get an actual official number from kraxel@bytesex.org 
398         (see /usr/src/linux/include/linux/videodev.h) */
399 #define VID_HARDWARE_HRT 36
400 
401 static struct video_device videodev_template = {
402         .owner = THIS_MODULE,
403         .name = "HRT Pixelsmart (PS512-8-PCI)",
404         .type = HRT_VID_TYPE,
405         .hardware = VID_HARDWARE_HRT,
406         .fops = &hrt_fops,
407 };
408 
409 /**
410  * I2C code follows
411  */
412 
413 /* 0x2001 = 8193 = 2^13+1 */
414 #define I2C_CONTROL_OFFSET 0x2001
415 
416 /* The word at 0x2001 provides access to the i2c bus of the HRT512-8
417    bit 0 = i2c clock signal ("scl" for short)
418    bit 1 = i2c data/address signal ("sda" for short)
419    bit 2 = "go" signal for hardware to generate a clock pulse,
420            once data is ready. This is a write-only bit.
421            It is cleared by hardware a few microseconds
422            after the software sets it high.
423  */
424 #define I2C_CONTROL(addr) (addr + I2C_CONTROL_OFFSET)
425 
426 static int hrt_i2c_attach_inform(struct i2c_client *client)
427 {
428         dprintk("hrt_i2c_attach_inform called\n");
429         return 0;
430 }
431 
432 #define I2C_HW_B_HRT  0xde
433 
434 static struct i2c_adapter hrt_i2c_adap_template = {
435         .owner             = THIS_MODULE,
436         I2C_DEVNAME("hrt"),
437         .id                = I2C_HW_B_HRT,
438         .class             = I2C_ADAP_CLASS_TV_ANALOG,
439         .client_register   = hrt_i2c_attach_inform,
440 };
441 
442 static struct i2c_client hrt_i2c_client_template = {
443         I2C_DEVNAME("hrt internal"),
444         .id       = -1,
445 };
446 
447 static int algo_control(struct i2c_adapter *adapter,
448                         unsigned int cmd, unsigned long arg);
449 static u32 bit_func(struct i2c_adapter *adap);
450 static int bit_xfer(struct i2c_adapter *i2c_adap,
451                     struct i2c_msg msgs[], int num);
452 
453 static struct i2c_algorithm i2c_hrt_algo = {
454         "HRT-specific I2C algorithm",
455         I2C_ALGO_EXP, /* XXX */
456         bit_xfer,
457         NULL,
458         NULL,                           /* slave_xmit           */
459         NULL,                           /* slave_recv           */
460         algo_control,                   /* ioctl                */
461         bit_func,                       /* functionality        */
462 };
463 
464 static int algo_control(struct i2c_adapter *adapter,
465                         unsigned int cmd, unsigned long arg)
466 {
467         return 0;
468 }
469 
470 static u32 bit_func(struct i2c_adapter *adap)
471 {
472         return I2C_FUNC_SMBUS_EMUL;
473 }
474 
475 static int bit_xfer(struct i2c_adapter *i2c_adap,
476                     struct i2c_msg msgs[], int num)
477 {
478         int i;
479         struct hrt *hrtdev = (struct hrt *)i2c_adap->algo_data;
480 
481         for (i = 0; i < msgs[0].len; i++) {
482                 hrt_i2c_send_byte(hrtdev->virtaddr, msgs[0].buf[i]);
483         }
484         return 0;
485 }
486 
487 static int i2c_hrt_add_bus(struct i2c_adapter *adap)
488 {
489         adap->id |= i2c_hrt_algo.id;
490         adap->algo = &i2c_hrt_algo;
491         i2c_add_adapter(adap);
492 
493         return 0;
494 }
495 
496 /**
497  * init_hrt_i2c - initialize and register the I2C adapter 
498  */
499 int init_hrt_i2c(struct hrt *hrtdev)
500 {
501         int result;
502 
503         memcpy(&hrtdev->i2c_adap, &hrt_i2c_adap_template,
504                sizeof(struct i2c_adapter));
505         memcpy(&hrtdev->i2c_algo, &i2c_hrt_algo,
506                sizeof(struct i2c_algorithm));
507         memcpy(&hrtdev->i2c_client, &hrt_i2c_client_template,
508                sizeof(struct i2c_client));
509 
510         sprintf(hrtdev->i2c_adap.dev.name, "HRT #%d", hrtdev->num);
511 
512         i2c_set_adapdata(&hrtdev->i2c_adap, hrtdev);
513         hrtdev->i2c_client.adapter = &hrtdev->i2c_adap;
514 
515         hrtdev->i2c_adap.algo_data = hrtdev;
516 
517         result = i2c_hrt_add_bus(&hrtdev->i2c_adap);
518         if (result != 0) {
519                 hrt_printk("i2c_hrt_add_bus failed\n");
520                 return result;
521         }
522 
523         result = i2c_init(hrtdev, saa7110_default_init_regs);
524         if (result != 0) {
525                 hrt_printk("i2c_init failed\n");
526                 return result;
527         }
528 
529         return result;
530 }
531 
532 /**
533  * Queue manipulation functions (basically wrappers for the Linux
534  * linked list functions)
535  */
536 void queue_add_tail(struct stream_buffer *streambuf,
537                     struct stream_buffer_queue *q)
538 {
539         unsigned long flags;
540 
541         if (streambuf == NULL || q == NULL) return;
542 
543         write_lock_irqsave(&q->lock, flags);
544         list_add_tail(&streambuf->node, &q->head);
545         q->length++;
546         write_unlock_irqrestore(&q->lock, flags);
547 }
548 
549 void queue_del(struct stream_buffer *streambuf,
550                struct stream_buffer_queue *q)
551 {
552         unsigned long flags;
553 
554         if (q == NULL) return;
555 
556         write_lock_irqsave(&q->lock, flags);
557         list_del(&streambuf->node);
558         q->length--;
559         write_unlock_irqrestore(&q->lock, flags);
560 }
561 
562 struct stream_buffer *queue_peek_head(struct stream_buffer_queue *q)
563 {
564         unsigned long flags;
565         struct stream_buffer *streambuf;
566 
567         if (q == NULL) return NULL;
568 
569         read_lock_irqsave(&q->lock, flags);
570         streambuf = list_entry(q->head.next, struct stream_buffer, node);
571         read_unlock_irqrestore(&q->lock, flags);
572 
573         return streambuf;
574 }
575 
576 struct stream_buffer *queue_del_head(struct stream_buffer_queue *q)
577 {
578         unsigned long flags;
579         struct stream_buffer *streambuf;
580 
581         if (q == NULL) return NULL;
582         if (!q->length) return NULL;
583 
584         read_lock_irqsave(&q->lock, flags);
585         streambuf = list_entry(q->head.next, struct stream_buffer, node);
586         list_del(&streambuf->node);
587         q->length--;
588         read_unlock_irqrestore(&q->lock, flags);
589         return streambuf;
590 }
591 
592 void __DEVEXIT hrt_remove(struct pci_dev *pci_dev)
593 {
594         dprintk("hrt_remove called\n");
595 }
596 
597 /**
598  * hrt_probe - check that we have a device as the specified address.
599  *  Assume the memory region is already mapped.
600  *  The address has to be a virtual address mapped to the device I/O space.
601  */
602 int hrt_probe(unsigned long addr)
603 {
604         unsigned char oldval1, oldval2, oldval3, newval2;
605         unsigned int oldaddr;
606 
607         /* save the old values at the address */
608         oldval1 = readb(HRT_CONTROL_REG + addr);
609         rmb();
610         oldaddr = readw(HRT_Y_LOW_REG + addr);
611         rmb();
612         
613         /* freeze the frame grabbing, immediately */
614         writeb(0x5B, HRT_CONTROL_REG + addr);
615         wmb();
616       
617         /* write a new value to the first byte in the first raster/row */
618         writew(0, HRT_Y_LOW_REG + addr);
619         wmb();
620         oldval2 = readb(addr);
621         rmb();
622         writeb(~oldval2, addr);
623         wmb();
624         
625         /* write oldval2 to the first byte of the next raster/row */
626         writew(1, HRT_Y_LOW_REG + addr);
627         wmb();
628         oldval3 = readb(addr);
629         rmb();
630         writeb(oldval2, addr);
631         wmb();
632         
633         /* read the value at the previous raster/row */
634         writew(0, HRT_Y_LOW_REG + addr);
635         wmb();
636         newval2 = readb(addr);
637         rmb();
638         
639         /* restore the old values */
640         writeb(oldval2, addr);
641         wmb();
642         writew(1, HRT_Y_LOW_REG + addr);
643         wmb();
644         writeb(oldval3, addr);
645         wmb();
646         writeb(oldaddr, HRT_Y_LOW_REG + addr);
647         wmb();
648         writeb(oldval1, HRT_CONTROL_REG + addr);
649         wmb();
650         
651         return (newval2 == (unsigned char)~oldval2);
652 }
653 
654 /**
655  * hrt_try_address - return 0 if there is a device
656  *  at the specified address; else return an error code.
657  *  If there is a device, map the device memory into the kernel
658  *  virtual memory space.
659  */
660 int hrt_try_address(struct hrt *hrtdev, unsigned long address) {
661         unsigned long virtual_addr;
662         int result;
663 
664         dprintk("hrt_try_address: 0x%lx\n", address);
665 
666         /* Reserve our memory range */
667         if (!request_mem_region(address, BYTES_NEEDED, "hrt")) {
668                 hrt_printk("I/O memory already in use\n");
669                 return -EBUSY;
670         }
671         virtual_addr = (unsigned long) ioremap_nocache(address, BYTES_NEEDED);
672 
673         /* Check that there is really a device at this address */
674         if (hrt_probe(virtual_addr)) {
675                 hrtdev->physaddr = address;
676                 hrtdev->virtaddr = virtual_addr;
677 
678                 /* Try to initialize the A/D converter */
679                 result = init_hrt_i2c(hrtdev);
680                 if (result < 0)
681                         goto failure;
682                 else
683                         return result;
684         } else {
685                 goto failure;
686         }
687 
688  failure:
689         /* Release memory and unmap */
690         iounmap((void *)virtual_addr);
691         release_mem_region(address, BYTES_NEEDED);
692         return -ENODEV;
693 }
694 
695 /**
696  * v4l1_ioctls - used for printing out ioctl codes in hrt_do_ioctl
697  */
698 static const char *v4l1_ioctls[] = {
699         "?", "CGAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT",
700         "SPICT", "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
701         "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
702         "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
703         "SMICROCODE", "GVBIFMT", "SVBIFMT"
704 };
705 
706 /**
707  * hrt_do_ioctl - handles ioctl's on /dev/video<n>
708  */
709 static int hrt_do_ioctl(struct inode *inode, struct file *file,
710                         unsigned int cmd, void *arg)
711 {
712         struct hrt_per_file *per_file = file->private_data;
713         struct hrt *hrtdev = per_file->hrtdev;
714         hrtdev->win = &per_file->win;
715 
716         /* This nice code copied from xawtv's libng.
717            It prints out the ioctl's that are done.
718          */
719         switch (_IOC_TYPE(cmd)) {
720         case 'v':
721                 dprintk("ioctl 0x%x (v4l1, VIDIOC%s)\n",
722                         cmd, (_IOC_NR(cmd) < ARRAY_SIZE(v4l1_ioctls)) ?
723                         v4l1_ioctls[_IOC_NR(cmd)] : "???");
724                 break;
725         case 'V':
726                 dprintk("ioctl 0x%x (v4l2, %s)\n",
727                         cmd, v4l2_ioctl_names[_IOC_NR(cmd)]);
728                 break;
729         default:
730                 dprintk("ioctl 0x%x (?)\n", cmd);
731         }
732 
733         switch (cmd) {
734 
735         /* Streaming ioctl's */
736         case VIDIOC_REQBUFS: {
737                 struct v4l2_requestbuffers *req = arg;
738 
739                 if (hrtdev->stream_buffers_mapped) {
740                         hrt_printk("REQBUFS - Can't request buffers if "
741                                    " buffers are already mapped\n");
742                         return -EPERM;
743                 }
744                 if (!mmap_request_buffers(hrtdev, req)) {
745                         hrt_printk("REQBUFS - Request of buffers failed\n");
746                         return -EINVAL;
747                 }
748                 return 0;
749         }
750         case VIDIOC_QUERYBUF: {
751                 struct v4l2_buffer *buf =
752                         (struct v4l2_buffer *) arg;
753                 int i;
754                 i = buf->index;
755                 if (i < 0 || i >= MAX_CAPTURE_BUFFERS ||
756                     !hrtdev->stream_buf[i].requested ||
757                     (buf->type & V4L2_BUF_TYPE_VIDEO_CAPTURE) !=
758                     (hrtdev->stream_buf[i].vidbuf.type & V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
759                         hrt_printk("QUERYBUF - bad parameter\n");
760                         return -EINVAL;
761                 }
762                 *buf = hrtdev->stream_buf[i].vidbuf;
763                 return 0;
764         }
765         case VIDIOC_QBUF: {
766                 struct v4l2_buffer *buf =
767                         (struct v4l2_buffer *) arg;
768                 
769                 if (!hrtdev->stream_buffers_mapped) {
770                         hrt_printk("QBUF - No buffers are mapped\n");
771                         return -EINVAL;
772                 }
773                 
774                 if (!hrt_queuebuffer(hrtdev, buf))
775                         return -EINVAL;
776                 return 0;
777         }
778         case VIDIOC_DQBUF: {
779                 struct v4l2_buffer *buf =
780                         (struct v4l2_buffer *) arg;
781                 
782                 if (!hrt_dequeuebuffer(hrtdev, buf)) {
783                         hrt_printk("DQBUF - failure to dequeue buffer\n");
784                         return -EINVAL;
785                 }
786                 return 0;
787         }
788         case VIDIOC_STREAMON: {
789                 __u32 *type = (__u32 *) arg;
790                 if (!hrt_streamon(hrtdev, *type))
791                         return -EINVAL;
792                 return 0;
793         }
794         case VIDIOC_STREAMOFF: {
795                 __u32 *type = (__u32 *) arg;
796                 hrt_streamoff(hrtdev, *type);
797                 return 0;
798         }
799 
800         /* Non-streaming ioctl's */
801         case VIDIOC_G_CROP: {
802                 struct v4l2_crop *crop = (struct v4l2_crop *) arg;
803 
804                 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
805                 crop->c = *(hrtdev->win);
806 
807                 return 0;
808         }
809         case VIDIOC_S_CROP: {
810                 struct v4l2_crop *crop = (struct v4l2_crop *) arg;
811 
812                 if (crop->c.height > HRT_HEIGHT) crop->c.height = HRT_HEIGHT;
813                 if (crop->c.height < 0) crop->c.height = 0;
814                 if (crop->c.width > HRT_WIDTH) crop->c.width = HRT_WIDTH;
815                 if (crop->c.width < 0) crop->c.width = 0;
816                 if (crop->c.top < 0) crop->c.top = 0;
817                 if (crop->c.top > HRT_HEIGHT) crop->c.top = 0;
818                 if (crop->c.left < 0) crop->c.left = 0;
819                 if (crop->c.left > HRT_WIDTH) crop->c.left = 0;
820 
821                 *(hrtdev->win) = crop->c;
822 
823                 return 0;
824         }
825         case VIDIOC_QUERYCAP: {
826                 struct v4l2_capability *b =
827                         (struct v4l2_capability *) arg;
828                 
829                 strcpy(b->driver, "hrt");
830                 strncpy(b->card, "PS 512-8-PCI", sizeof(b->card));
831                 sprintf(b->bus_info,"PCI:%s", hrtdev->pcidev->slot_name);
832                 b->version = KERNEL_VERSION(0, 0, 3);
833 
834                 if (disable_streaming) {
835                         b->capabilities =
836                                 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
837                 } else {
838                         b->capabilities =
839                                 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
840                                 V4L2_CAP_STREAMING;
841                 }
842                 return 0;
843         }
844         case VIDIOC_ENUM_FMT: {
845                 struct v4l2_fmtdesc *f = arg;
846                 enum v4l2_buf_type type = f->type;
847                 int index = f->index;
848                 
849                 /* There's only one format */
850                 if (index > 0)
851                         return -EINVAL;
852                 
853                 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
854                         memset(f, 0, sizeof(*f));
855                         f->index = index;
856                         f->type = type;
857                         f->pixelformat = V4L2_PIX_FMT_GREY;
858                         strcpy(f->description, "Grey");
859                         return 0;
860                 } else {
861                         return -EINVAL;
862                 }
863         }
864         case VIDIOC_TRY_FMT:
865         case VIDIOC_S_FMT:
866         case VIDIOC_G_FMT: {
867                 struct v4l2_format *f = arg;
868 
869                 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
870                         memset(&f->fmt.pix, 0,
871                                sizeof(struct v4l2_pix_format));
872                         f->fmt.pix.width = hrtdev->win->width;
873                         f->fmt.pix.height = hrtdev->win->height;
874                         f->fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
875                         f->fmt.pix.bytesperline = hrtdev->bytesperline;
876                         f->fmt.pix.sizeimage = hrtdev->framesize;
877                         return 0;
878                 } else {
879                         return -EINVAL;
880                 }
881         }
882         case VIDIOC_G_STD: {
883                 v4l2_std_id *id = arg;
884 
885                 *id = V4L2_STD_NTSC;
886                 return 0;
887         }
888         case VIDIOC_S_STD: {
889                 /* There's only one standard that we support, NTSC */
890                 return 0;
891         }
892         case VIDIOC_QUERYCTRL: {
893                 struct v4l2_queryctrl *c = arg;
894                 int i;
895 
896                 for (i = 0; i < ARRAY_SIZE(hrt_ctls); i++)
897                         if (hrt_ctls[i].id == c->id)
898                                 break;
899                 
900                 /* Didn't find the id */
901                 if (i == ARRAY_SIZE(hrt_ctls)) {
902                         return -EINVAL;
903                 }
904                 
905                 *c = hrt_ctls[i];
906                 return 0;
907         }
908         case VIDIOC_G_CTRL: {
909                 struct v4l2_control *c = arg;
910                 int i;
911 
912                 for (i = 0; i < ARRAY_SIZE(hrt_ctls); i++)
913                         if (hrt_ctls[i].id == c->id)
914                                 break;
915                 
916                 if (i == sizeof(hrt_ctls))
917                         return -EINVAL;
918                 
919                 if (c->id == V4L2_CID_BRIGHTNESS) {
920                         c->value = hrtdev->regvals[HRT_BRIGHTNESS_REG];
921                 } else if (c->id == V4L2_CID_CONTRAST) {
922                         c->value = hrtdev->regvals[HRT_CONTRAST_REG];
923                 } else {
924                         return -EINVAL;
925                 }
926                 
927                 return 0;
928         }
929         case VIDIOC_S_CTRL: {
930                 struct v4l2_control *c = arg;
931                 int i;
932                 
933                 for (i = 0; i < sizeof(hrt_ctls); i++)
934                         if (hrt_ctls[i].id == c->id)
935                                 break;
936                 if (i == sizeof(hrt_ctls))
937                         return -EINVAL;
938                 
939                 if (c->id == V4L2_CID_BRIGHTNESS) {
940                         if (c->value > 255)
941                                 return -EINVAL;
942                         c->value = i2c_set_reg(hrtdev, HRT_BRIGHTNESS_REG,
943                                                c->value);
944                 } else if (c->id == V4L2_CID_CONTRAST) {
945                         if (c->value > 255)
946                                 return -EINVAL;
947                         c->value = i2c_set_reg(hrtdev, HRT_CONTRAST_REG,
948                                                c->value);
949                 } else {
950                         return -EINVAL;
951                 }
952                 return 0;
953         }
954         case VIDIOC_ENUMINPUT: {
955                 struct v4l2_input *i = arg;
956                 unsigned int n;
957                 
958                 /* Only one input */
959                 if (i->index > 0)
960                         return -EINVAL;
961                 
962                 n = i->index;
963                 memset(i, 0, sizeof(*i));
964                 i->index = n;
965                 i->type = V4L2_INPUT_TYPE_CAMERA;
966                 sprintf(i->name, "NTSC Camera");
967                 
968                 return 0;
969         }
970         case VIDIOC_ENUMSTD: {
971                 struct v4l2_standard *e = arg;
972                 struct v4l2_fract fract = { 1001, 30000 };
973                 unsigned int index = e->index;
974                 
975                 /* One standard (NTSC) */
976                 if (index > 0)
977                         return -EINVAL;
978 
979                 e->id = V4L2_STD_NTSC_M;
980                 e->index = index;
981                 strcpy(e->name, "NTSC");
982                 e->frameperiod = fract;
983                 e->framelines = hrtdev->win->height;
984                 
985                 return 0;
986         }
987         case VIDIOC_G_PARM: {
988                 struct v4l2_streamparm *parm = arg;
989                 struct v4l2_standard s;
990                 
991                 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
992                         return -EINVAL;
993                 memset(parm, 0, sizeof(*parm));
994 
995                 v4l2_video_std_construct(&s, V4L2_STD_NTSC,
996                                          "NTSC");
997                 parm->parm.capture.timeperframe = s.frameperiod;
998                 return 0;
999         }
1000         case VIDIOC_G_INPUT: {
1001                 return 0;
1002         }
1003         case VIDIOC_S_INPUT: {
1004                 return 0;
1005         }
1006         default:
1007                 return -ENOIOCTLCMD;
1008         }
1009 }
1010 
1011 /**
1012  * hrt_ioctl - handler for ioctl's on /dev/video<n>. Tries the private
1013  *  ioctl's first, via hrt_do_private_ioctl().
1014  */
1015 static int hrt_ioctl(struct inode *inode, struct file *file,
1016                             unsigned int cmd, unsigned long arg)
1017 {
1018         struct hrt_per_file *per_file = file->private_data;
1019         struct hrt *hrtdev = per_file->hrtdev;
1020         int ret;
1021         hrtdev->win = &per_file->win;
1022 
1023         dprintk("ioctl arg = %lx\n", arg);
1024 
1025         down(&hrtdev->sem);
1026 
1027         ret = hrt_do_private_ioctl(hrtdev, cmd, arg);
1028         if (ret != -EINVAL) {
1029                 up(&hrtdev->sem);
1030                 return ret;
1031         }
1032         ret = video_usercopy(inode, file, cmd, arg, hrt_do_ioctl);
1033         up(&hrtdev->sem);
1034         return ret;
1035 }
1036 
1037 /**
1038  * hrt_read - depends on timer_func to change the field_bit.
1039  *  Reads at most about 23.5 FPS on this 400MHz machine without displaying,
1040  *  and it reads at about 13 FPS under 2.4 (16 FPS under 2.6) 
1041  *  with displaying (using hrtdemo).
1042  */
1043 static int hrt_read(struct file *file, char *buf, size_t count, loff_t *ppos)
1044 {
1045         struct hrt_per_file *per_file = file->private_data;
1046         struct hrt *hrtdev = per_file->hrtdev;
1047         int len;
1048         int old_field;
1049 
1050         hrtdev->win = &per_file->win;
1051         len = min((size_t) (hrtdev->win->height * hrtdev->bytesperline), count);
1052         down_interruptible(&hrtdev->sem);
1053         old_field = hrtdev->field_bit;
1054 
1055         /* Grab a field */
1056         grab_field(hrtdev, hrtdev->framedata, old_field);
1057 
1058         /* Go to sleep until the field bit changes */
1059         while (old_field == hrtdev->field_bit) {
1060                 up(&hrtdev->sem);
1061                 if (file->f_flags & O_NONBLOCK)
1062                         return -EAGAIN;
1063 
1064                 if (wait_event_interruptible
1065                     (hrtdev->waitqueue, (old_field != hrtdev->field_bit)))
1066                         return -ERESTARTSYS;
1067 
1068                 if (down_interruptible(&hrtdev->sem))
1069                         return -ERESTARTSYS;
1070         }
1071         /* Grab the other field */
1072         grab_field(hrtdev, hrtdev->framedata, hrtdev->field_bit);
1073 
1074         if (copy_to_user(buf, hrtdev->framedata, len)) {
1075                 up(&hrtdev->sem);
1076                 return -EFAULT;
1077         }
1078         up(&hrtdev->sem);
1079 
1080         return len;
1081 }
1082 
1083 /**
1084  * streaming_fieldchange - if streaming is on, this function is called
1085  *  from timer_func when the field_bit changes
1086  */
1087 static inline void streaming_fieldchange(struct hrt *hrtdev)
1088 {
1089         struct stream_buffer *streambuf;
1090 
1091         /* Capture list is empty */
1092         if (!hrtdev->capture_list.length) {
1093                 return;
1094         }
1095 
1096         streambuf = queue_peek_head(&hrtdev->capture_list);
1097         
1098         /* Sanity checks- don't want to do anything bad
1099            in interrupt mode */
1100         if (streambuf == NULL) {
1101                 hrt_printk("streambuf is NULL!\n");
1102                 return;
1103         }
1104         if (streambuf->vaddress == NULL) {
1105                 hrt_printk("vaddress is NULL!\n");
1106                 return;
1107         }
1108         if (hrtdev->framedata == NULL) {
1109                 hrt_printk("framedata is NULL!\n");
1110                 return;
1111         }
1112 
1113         grab_field(hrtdev, streambuf->vaddress,
1114                    !hrtdev->field_bit);
1115         streambuf->fields_grabbed |= (!hrtdev->field_bit) + 1;
1116         
1117         /* We have a complete frame */
1118         if (streambuf->fields_grabbed == 3) {
1119                 streambuf->fields_grabbed = 0;
1120 
1121                 streambuf->vidbuf.flags |= 
1122                         V4L2_BUF_FLAG_DONE |
1123                         V4L2_BUF_FLAG_KEYFRAME;
1124         
1125                 /* Move the buffer to the done queue */
1126                 queue_del(streambuf, &hrtdev->capture_list);
1127                 queue_add_tail(streambuf, &hrtdev->done_list);
1128 
1129                 hrtdev->field_bit = !hrtdev->field_bit;
1130                 wake_up(&hrtdev->waitqueue);
1131         }
1132 }
1133 
1134 /**
1135  * timer_func - the crux of the driver. This function is called every timer
1136  *  tick and checks to see if the field bit has changed. If it has, we
1137  *  either wake up sleeping readers or call streaming_fieldchange().
1138  */
1139 void timer_func(unsigned long ptr)
1140 {
1141         int i;
1142 
1143         for (i = 0; i < num_hrtdevs; i++) {
1144                 int new_field;
1145 
1146                 new_field = readb(hrtdevs[i].virtaddr + HRT_CONTROL_REG) & 1;
1147                 if (new_field != hrtdevs[i].field_bit) {
1148                         hrtdevs[i].field_bit = new_field;
1149 
1150                         if (hrtdevs[i].streaming) {
1151                                 streaming_fieldchange(&hrtdevs[i]);
1152                         } else {
1153                                 wake_up(&hrtdevs[i].waitqueue);
1154                         }
1155                 }
1156         }
1157 
1158         timer.expires = jiffies + (HZ/100);
1159         add_timer(&timer);
1160 }
1161 
1162 /**
1163  * per_file_init - allocates and initializes per-file data, which
1164  *  is currently just the ROI (region-of-interest) subwindow
1165  *  and a pointer back to this hrtdev
1166  */
1167 struct hrt_per_file *per_file_init(struct hrt *hrtdev)
1168 {
1169         struct hrt_per_file *per_file = vmalloc(sizeof(*per_file));
1170 
1171         per_file->hrtdev = hrtdev;
1172         per_file->win.height = HRT_HEIGHT;
1173         per_file->win.width = HRT_WIDTH;
1174         per_file->win.left = 0;
1175         per_file->win.top = 0;
1176 
1177         return per_file;
1178 }
1179 
1180 /**
1181  * hrt_open - called upon an open() on /dev/video<n>
1182  */
1183 int hrt_open(struct inode *inode, struct file *file)
1184 {
1185         struct video_device *vfl = video_devdata(file);
1186         struct hrt *hrtdev = vfl->priv;
1187         int retval = 0;
1188 
1189         down(&hrtdev->sem);
1190 
1191         /* Increment users */
1192         users++;
1193 
1194         if (exclusive && users) {
1195                 retval = -EBUSY;
1196         } else {
1197                 /* Initialize per-file data */
1198                 file->private_data = per_file_init(hrtdev);
1199 
1200                 /* Get the timer going */
1201                 if (!timer_running) {
1202                         timer.expires = jiffies + (HZ/100);
1203                         add_timer(&timer);
1204                         timer_running = 1;
1205                 }
1206                 
1207                 /* Start off not in streaming mode */
1208                 hrtdev->streaming = 0;
1209 
1210         }
1211         up(&hrtdev->sem);
1212 
1213         return retval;
1214 }
1215 
1216 /**
1217  * hrt_release - basically just stops the timer because it isn't necessary
1218  *  if no process has /dev/video<n> open and frees the per-file data
1219  */
1220 static int hrt_release(struct inode *inode, struct file *file)
1221 {
1222         struct video_device *vfl = video_devdata(file);
1223         struct hrt *hrtdev = vfl->priv;
1224 
1225         down(&hrtdev->sem);
1226 
1227         /* Decrement users */
1228         users--;
1229 
1230         /* Tell the timer to stop if necessary */
1231         if (!users) {
1232                 del_timer_sync(&timer);
1233                 timer_running = 0;
1234         }
1235 
1236         /* Free our private data */
1237         vfree(file->private_data);
1238 
1239         up(&hrtdev->sem);
1240 
1241         return 0;
1242 }
1243 
1244 /**
1245  * hrt_poll - handles poll() and select() on /dev/video<n>
1246  */
1247 unsigned int hrt_poll(struct file *file, poll_table *wait)
1248 {
1249         struct hrt_per_file *per_file = file->private_data;
1250         struct hrt *hrtdev = per_file->hrtdev;
1251 
1252         if (!hrtdev->streaming) {
1253                 poll_wait(file, &hrtdev->waitqueue, wait);
1254         } else {
1255                 /* Go to sleep if the done list is empty */
1256                 while (!hrtdev->done_list.length) {
1257                         up(&hrtdev->sem);
1258                         if (wait_event_interruptible
1259                             (hrtdev->waitqueue, (hrtdev->done_list.length)))
1260                                 return -ERESTARTSYS;
1261 
1262                         if (down_interruptible(&hrtdev->sem))
1263                                 return -ERESTARTSYS;
1264                 }
1265         }
1266 
1267         return POLLIN | POLLRDNORM;
1268 }
1269 
1270 /**
1271  * hrt_dev_init - initializes a device. Assumes hrtdev->virtaddr
1272  *  has been set to an ioremap'd address.
1273  */
1274 int hrt_dev_init(struct hrt *hrtdev)
1275 {
1276         /* We don't start in streaming mode */
1277         hrtdev->streaming = 0;
1278 
1279         /* Make the lists empty */
1280         INIT_LIST_HEAD(&hrtdev->capture_list.head);
1281         INIT_LIST_HEAD(&hrtdev->done_list.head);
1282         hrtdev->capture_list.length = hrtdev->done_list.length = 0;
1283 
1284         /* Initialize the spinlocks to protect the lists */
1285         hrtdev->capture_list.lock = RW_LOCK_UNLOCKED;
1286         hrtdev->done_list.lock = RW_LOCK_UNLOCKED;
1287 
1288         /* Register with V4L */
1289         hrtdev->video_dev = videodev_template;
1290         if (video_register_device(&hrtdev->video_dev, VFL_TYPE_GRABBER,
1291                                   video_minor) < 0) {
1292                 hrt_printk(KERN_ERR "Unable to register video device\n");
1293                 return -ENODEV;
1294         }
1295 
1296         /* Set private data (to access through struct file) */
1297         hrtdev->video_dev.priv = hrtdev;
1298 
1299         /* Set the constants */
1300         hrtdev->bytesperline = HRT_BYTES_PER_LINE;
1301         hrtdev->framesize = HRT_FRAMESIZE;
1302 
1303         /* Initialize the wait queue (for poll) */
1304         init_waitqueue_head(&hrtdev->waitqueue);
1305 
1306         /* Initialize semaphore */
1307         sema_init(&hrtdev->sem, 1);
1308 
1309         /* Read the field id bit (should be 0 since we're shouldn't be
1310            in live mode yet) */
1311         hrtdev->field_bit = readb(hrtdev->virtaddr + HRT_CONTROL_REG) & 1;
1312 
1313         /* Put the card into Live mode */
1314         writeb(HRT_LIVE_CMD, hrtdev->virtaddr + HRT_CONTROL_REG);
1315 
1316         /* Allocate frame buffer memory */
1317         hrtdev->framedata = (unsigned char *) vmalloc(HRT_FRAMESIZE);
1318 
1319         if (hrtdev->framedata == NULL) {
1320                 hrt_printk(KERN_ERR "Unable to allocate memory\n");
1321                 video_unregister_device(&hrtdev->video_dev);
1322                 return -ENOMEM;
1323         }
1324 
1325         /* Set up our timer routine. It should expire 100 times
1326            a second (every 1 jiffy on 2.4, every 10 jiffies on 2.6) */
1327         init_timer(&timer);
1328         timer.function = timer_func;
1329         timer.expires = jiffies + (HZ/100);
1330 
1331         return 0;
1332 }
1333 
1334 /**
1335  * hrt_pci_probe - try to initialize one PCI device
1336  */
1337 static int __DEVINIT hrt_pci_probe(struct pci_dev *dev,
1338                                    const struct pci_device_id *pci_id)
1339 {
1340         struct hrt *hrtdev = NULL;
1341 
1342         /* Check that the device is one of those we recognize */
1343         if (dev->device != HRT_DEVICE_ID_GRAY) {
1344                 hrt_printk("Unknown HRT device: %d\n", dev->device);
1345                 return -ENODEV;
1346         }
1347 
1348         /* Try to find a free device descriptor */
1349         if (num_hrtdevs == HRT_MAX_DEVS) {
1350                 hrt_printk("More than %d HRT devices\n", HRT_MAX_DEVS);
1351                 return -ENOMEM;
1352         }
1353         hrtdev = &hrtdevs[num_hrtdevs];
1354         hrtdev->num = num_hrtdevs;
1355 
1356         /* Probe the device to get the base address */
1357         if (pci_enable_device(dev)) {
1358                 hrt_printk("Couldn't enable device\n");
1359                 return -EIO;
1360         }
1361         
1362         if (!hrt_try_address(hrtdev, pci_resource_start(dev, 0))) {
1363                 dprintk("Found card at 0x%lx\n", pci_resource_start(dev, 0));
1364 
1365                 /* Success! We found the card 
1366                    So reserve our struct hrt */
1367                 num_hrtdevs++;
1368                 /* Copy the PCI device struct */
1369                 hrtdev->pcidev = dev;
1370                 /* And hrt_dev_init does the rest */
1371                 return hrt_dev_init(hrtdev);
1372         }
1373 
1374         /* Failure; couldn't find it */
1375         return -ENODEV;
1376 }
1377 
1378 /**
1379  * hrt_dev_cleanup - deallocate per-card resources
1380  */
1381 void hrt_dev_cleanup(struct hrt *hrtdev)
1382 {
1383         video_unregister_device(&hrtdev->video_dev);
1384         release_mem_region(hrtdev->physaddr, BYTES_NEEDED);
1385         iounmap((unsigned char *)hrtdev->virtaddr);
1386         vfree(hrtdev->framedata);
1387         if (timer_running)
1388                 del_timer_sync(&timer);
1389         i2c_del_adapter(&hrtdev->i2c_adap);
1390 }
1391 
1392 /**
1393  * init_module - where it all starts. Mainly performs probing.
1394  */
1395 int __init init_module(void)
1396 {
1397         int result;
1398         unsigned int i;
1399 
1400         result = pci_module_init(&hrt_pci_driver);
1401         if (result < 0)
1402                 no_pci = 1;
1403 
1404         for (i = 0; i < ARRAY_SIZE(hrt_addresses); i++) {
1405                 if (num_hrtdevs == HRT_MAX_DEVS) {
1406                         hrt_printk("More than %d HRT devices\n", HRT_MAX_DEVS);
1407                         return -ENOMEM;
1408                 }
1409                 if (!hrt_try_address(&hrtdevs[num_hrtdevs], hrt_addresses[i])) {
1410                         result = hrt_dev_init(&hrtdevs[num_hrtdevs]);
1411                         if (result < 0)
1412                                 return result;
1413                         num_hrtdevs++;
1414                 }
1415         }
1416 
1417         if (num_hrtdevs == 0) {
1418                 hrt_printk("No HRT cards detected.\n");
1419                 return -ENODEV;
1420         }
1421         hrt_printk("Number of HRT devices found: %d\n", num_hrtdevs);
1422 
1423         return 0;
1424 }
1425 
1426 /**
1427  * hrt_do_private_ioctl - handle private ioctl's on device
1428  */
1429 static int hrt_do_private_ioctl(struct hrt *hrtdev, unsigned int cmd,
1430                                 unsigned long arg)
1431 {
1432         if (cmd == IOC_HRT_GET_MAGIC_MMAP_OFFSET) {
1433                 /* When mmap() is called on /dev/video<n>
1434                    with the offset returned by this ioctl,
1435                    the device's memory will be mapped directly-
1436                    this is for userspace drivers */
1437                 return HRT_MAGIC_MMAP_OFFSET;
1438         } else if (cmd == IOC_HRT_SET_I2CREG) {
1439                 struct i2c_regval r;
1440 
1441                 if (copy_from_user(&r, (void *) arg, sizeof(r))) {
1442                         return -EFAULT;
1443                 }
1444                 i2c_set_reg(hrtdev, r.reg, r.val);
1445                 return 0;
1446         } else if (cmd == IOC_HRT_GET_I2CREG) {
1447                 struct i2c_regval r;
1448 
1449                 r.val = hrtdev->regvals[r.reg];
1450                 if (copy_to_user((void *) arg, &r, sizeof(r))) {
1451                         return -EFAULT;
1452                 }
1453                 return 0;
1454         } else if (cmd == IOC_HRT_SET_I2CREGS) {
1455                 unsigned char len;
1456                 int retval;
1457                 unsigned char *buf;
1458 
1459                 /* Stop the timer first */
1460                 del_timer_sync(&timer);
1461                 timer_running = 0;
1462 
1463                 /* First byte is the length of the array */
1464                 if (copy_from_user(&len, (void *) arg, 1))
1465                         return -EFAULT;
1466                 dprintk("size of array = %d\n", len);
1467 
1468                 buf = vmalloc(len + 1);
1469                 if (copy_from_user(buf, (void *) arg, len)) {
1470                         vfree(buf);
1471                         return -EFAULT;
1472                 }
1473                 retval = i2c_init(hrtdev, buf); 
1474                 vfree(buf);
1475 
1476                 /* Start the timer again */
1477                 timer.expires = jiffies + (HZ/100);
1478                 add_timer(&timer);
1479                 timer_running = 1;
1480 
1481                 return retval;
1482         }
1483         return -EINVAL;
1484 }
1485 
1486 /**
1487  * hrt_direct_mmap - provides direct access to the card's memory
1488  */
1489 static int hrt_direct_mmap(struct file *file,
1490                            struct vm_area_struct *vma)
1491 {
1492         struct hrt_per_file *per_file = file->private_data;
1493         struct hrt *hrtdev = per_file->hrtdev;
1494 
1495         dprintk("hrt_direct_mmap\n");
1496 
1497         vma->vm_flags |= (VM_IO | VM_RESERVED);
1498 
1499         /* Just map the pages directly */
1500 
1501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
1502         if (remap_page_range(
1503 #else
1504         if (remap_page_range(vma,
1505 #endif
1506              vma->vm_start, hrtdev->physaddr, vma->vm_end - vma->vm_start,
1507              vma->vm_page_prot))
1508                 return -EAGAIN;
1509 
1510         return 0;
1511 }
1512 
1513 /**
1514  * cleanup_module - called when we're unloaded of course
1515  */
1516 void __exit cleanup_module(void)
1517 {
1518         int i;
1519 
1520         /* Release our resouces */
1521         if (!no_pci)
1522                 pci_unregister_driver(&hrt_pci_driver);
1523         for (i = 0; i < num_hrtdevs; i++) {
1524                 hrt_dev_cleanup(&hrtdevs[i]);
1525         }
1526 }
1527 
1528 /**
1529  * grab_field - reads a field (half a frame) from the card's memory.
1530  *  Which field (0 or 1) is determined by the parity parameter.
1531  *  It stores the data into the framedata parameter, which is usually
1532  *  the same as hrtdev->framedata, except when we're doing streaming,
1533  *  in which case it's a stream buffer's vaddress field.
1534  */
1535 void inline grab_field(struct hrt *hrtdev, unsigned char *framedata,
1536                        int parity)
1537 {
1538         int i;
1539         int linelen;
1540         int left = hrtdev->win->left;
1541 
1542         /* XXX: workaround for strange skewing etc */
1543 /*      linelen = hrtdev->win->width - hrtdev->win->left;*/
1544         linelen = hrtdev->win->width;
1545 
1546         if (linelen % 4)
1547                 linelen += 4 - linelen % 4;
1548 
1549         if (left % 4)
1550                 left += 4 - left % 4;
1551 
1552         for (i = (!parity) + hrtdev->win->top; i < hrtdev->win->height;
1553              i += 2) {
1554                 /* Move the y register (current line) */
1555                 writew(i, hrtdev->virtaddr + HRT_Y_LOW_REG);
1556                 wmb();
1557                 memcpy_fromio(framedata + (i - hrtdev->win->top) * linelen,
1558                               hrtdev->virtaddr + left,
1559                               linelen);
1560         }
1561 }
1562 
1563 /***
1564  * Dr. Baker's I2C routines 
1565  ***/
1566 
1567 /* HRT_CONTROL_OFFSET is the offset of the I2C bus control port
1568    0x2000 = 8192 = 2^13 */
1569 #define HRT_CONTROL_OFFSET 0x2000
1570 #define HRT_CONTROL(addr) (addr + HRT_CONTROL_OFFSET)
1571 
1572 /* bit 7 at 0x2000 (the HRT512-8 control register) tells whether
1573    the CPU is sending data across the I2C bus */
1574 #define I2C_BUSY(addr) (!(readb(HRT_CONTROL(addr)) & 0x80))
1575 
1576 /* 0x2001 = 8193 = 2^13+1 */
1577 #define I2C_CONTROL_OFFSET 0x2001
1578 
1579 /* The word at 0x2001 provides access to the i2c bus of the HRT512-8
1580    bit 0 = i2c clock signal ("scl" for short)
1581    bit 1 = i2c data/address signal ("sda" for short)
1582    bit 2 = "go" signal for hardware to generate a clock pulse,
1583            once data is ready. This is a write-only bit.
1584            It is cleared by hardware a few microseconds
1585            after the software sets it high.
1586  */
1587 #define I2C_CONTROL(addr) (addr + I2C_CONTROL_OFFSET)
1588 
1589 #define I2C_POKE(addr,data) { writeb(data,I2C_CONTROL(addr)); wmb(); }
1590 #define I2C_PEEK(addr) (readb(I2C_CONTROL(addr)))
1591 
1592 #define I2C_00(addr) { writeb(0,I2C_CONTROL(addr)); wmb(); }
1593 #define I2C_10(addr) { writeb(1,I2C_CONTROL(addr)); wmb(); }
1594 #define I2C_01(addr) { writeb(2,I2C_CONTROL(addr)); wmb(); }
1595 #define I2C_11(addr) { writeb(3,I2C_CONTROL(addr)); wmb(); }
1596 
1597 static int i2c_send_byte(struct hrt *hrtdev, unsigned char byte)
1598 {
1599         return i2c_smbus_write_byte(&hrtdev->i2c_client, byte);
1600 }
1601 
1602 /**
1603  * i2c_start - sends 'start' command onto i2c bus to be recieved by all
1604  *  devices on bus. (The HRT512-8 only has the on A/D on the i2c bus.)
1605  *  This tells all devices on the i2c bus to prepare for an address
1606  *  phase, i.e., that one of them will be addressed next.
1607  */
1608 static inline void i2c_start(unsigned long addr)
1609 {
1610         I2C_00(addr);
1611         I2C_01(addr);
1612         I2C_11(addr);
1613         I2C_10(addr);
1614         I2C_00(addr);
1615 }
1616 
1617 /**
1618  * i2c_stop - tells the selected device (A/D) that transmission has
1619  *  completed and the bus is free.
1620  */
1621 static inline void i2c_stop(unsigned long addr)
1622 {
1623         I2C_00(addr);
1624         I2C_10(addr);
1625         I2C_11(addr);
1626         I2C_01(addr);
1627         I2C_11(addr);
1628 }
1629 
1630 /**
1631  * hrt_i2c_send_byte - sends a byte to the A/D.
1632  *  The parameter addr is the mapped I/O base address of a device.
1633  */
1634 static int hrt_i2c_send_byte(unsigned long addr, unsigned char data)
1635 {
1636         char bitpos;
1637         unsigned char sda, sda_slc;
1638         unsigned long timeout;
1639 
1640         for (bitpos = 7; bitpos >= 0; bitpos--) {
1641                 /* send a bit to the A/D device;
1642                    clock must be low at this time.
1643                    first, get the next bit from data value */
1644                 sda = (data & (1 << bitpos)) >> bitpos;
1645                 sda_slc = sda << 1;
1646                 /* put data bit on bus and take clock low (b0=0) */
1647                 I2C_POKE(addr, sda_slc);
1648                 /* send the go signal to initiate clock pulse */
1649                 I2C_POKE(addr, sda_slc + 4);
1650                 if (I2C_BUSY(addr)) {
1651                         timeout = jiffies + HZ / 10;
1652                         while (I2C_BUSY(addr)) {        /* spin */
1653                                 if (jiffies > timeout) {
1654                                         hrt_printk("I2C bus timeout\n");
1655                                         goto failure;
1656                                 }
1657                         }
1658                 }
1659         }
1660         I2C_01(addr);
1661         /* leave the sda line at high impedance
1662            (bit0=0 clock, bit1=1 data/high impedance) */
1663 
1664         /* check for ack from A/D after each byte has been sent */
1665         I2C_11(addr);
1666         if ((I2C_PEEK(addr) & 2) == 2) {
1667                 /* error: no ACK after Data Byte transmision */
1668                 hrt_printk("No I2C ACK\n");
1669                 goto failure;
1670         }
1671         I2C_01(addr);           /* return the bus to high impediance */
1672         return 0;
1673       failure:
1674         /* This is only a gesture toward cleaning up;
1675            if there is a failure we probably have a broken
1676            device. */
1677         I2C_01(addr);           /* return the bus to high impedance */
1678         i2c_stop(addr);
1679         return -1;
1680 }
1681 
1682 /**
1683  * i2c_init - set initial register values for the 
1684  *  SAA7110 A/D converter (digitizer), which is on an
1685  *  I2C bus. If an array of values is given, use it;
1686  *  otherwise, use the default set of values. See the
1687  *  comment on the declaration of saa7110_default_init_regs
1688  *  for the format of the argument.
1689  *  The parameter addr is the mapped I/O base address of a device.
1690  *
1691  *  NB that the first byte of the array should be the size of the array.
1692  */
1693 static int i2c_init(struct hrt *hrtdev,
1694                     const unsigned char *init_regs)
1695 {
1696         unsigned int i, lastreg, len;
1697         unsigned long addr = hrtdev->virtaddr;
1698 
1699         if (!init_regs)
1700                 init_regs = saa7110_default_init_regs;
1701 
1702         /* The length of the array is contained in the first byte */
1703         len = init_regs[0];
1704         init_regs++;
1705 
1706         i2c_start(addr);
1707         /* Tell the digitizer that it has been selected
1708            for data transmission. */
1709         if (i2c_send_byte(hrtdev, HRT_AD_DEVICE_ID))
1710                 return -1;
1711 
1712         /* Set the selected device's internal address register
1713            pointer to zero.  Each subsequent data write will
1714            autoincrement the device's address register pointer. */
1715         if (i2c_send_byte(hrtdev, 0))
1716                 return -1;
1717         lastreg = 0;
1718 
1719         for (i = 0; i < len; i += 2) {
1720                 if (init_regs[i] > HRT_NUMREGS) {
1721                         hrt_printk("Register number 0x%x out of range!\n",
1722                                    init_regs[i]);
1723                         return -1;
1724                 }
1725                 if (init_regs[i] != lastreg) {
1726                         /* there is a gap in the sequence */
1727                         i2c_stop(addr);
1728                         i2c_start(addr);
1729                         /* resend device address byte */
1730                         if (i2c_send_byte(hrtdev, HRT_AD_DEVICE_ID))
1731                                 return -1;
1732                         lastreg = init_regs[i];
1733 
1734                         /* send new register address byte */
1735                         if (i2c_send_byte(hrtdev, init_regs[i]))
1736                                 return -1;
1737                 }
1738                 lastreg++;
1739                 /* send register value */
1740                 if (i2c_send_byte(hrtdev, init_regs[i + 1]))
1741                         return -1;
1742 
1743                 /* Keep track of the values in the registers */
1744                 hrtdev->regvals[init_regs[i]] = init_regs[i + 1];
1745         }
1746         i2c_stop(addr);
1747         return 0;
1748 }
1749 
1750 /**
1751  * i2c_set_reg - set an I2C register, reg, to value val
1752  */
1753 int i2c_set_reg(struct hrt *hrtdev, int reg, unsigned char val)
1754 {
1755         unsigned long addr = hrtdev->virtaddr;
1756 
1757         i2c_start(addr);
1758         i2c_send_byte(hrtdev, HRT_AD_DEVICE_ID);
1759         i2c_send_byte(hrtdev, reg);
1760         i2c_send_byte(hrtdev, val);
1761         i2c_stop(addr);
1762 
1763         hrtdev->regvals[reg] = val;
1764 
1765         return val;
1766 }
1767 
1768 /***
1769  * Veena and Arthi's streaming implementation follows below.
1770  * It is based on Bill Dirks' v4l2cap.c.
1771  ***/
1772 
1773 /**
1774  * hrt_queuebuffer - called by the QBUF ioctl.
1775  * Adds a buffer to the capture queue.
1776  */ 
1777 int hrt_queuebuffer(struct hrt *hrtdev,
1778                     struct v4l2_buffer *vidbuf) 
1779 {
1780         int i = vidbuf->index;
1781         struct stream_buffer *buf = NULL;
1782 
1783         if (!hrtdev->stream_buffers_mapped) {
1784                 hrt_printk("QBUF - no buffers mapped\n");
1785                 return 0;
1786         }
1787         if (vidbuf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1788                 hrt_printk("QBUF - wrong type (type = %x)\n", vidbuf->type);
1789                 return 0;
1790         }
1791         if (i < 0 || i >= MAX_CAPTURE_BUFFERS
1792              || !hrtdev->stream_buf[i].requested) {
1793                 hrt_printk("QBUF - buffer index %d is out of range\n", i);
1794                 return 0;
1795         }
1796         buf = &hrtdev->stream_buf[i];
1797         if (!(buf->vidbuf.flags & V4L2_BUF_FLAG_MAPPED)) {
1798                 hrt_printk("QBUF - buffer %d is not mapped\n", i);
1799                 return 0;
1800         }
1801         if ((buf->vidbuf.flags & V4L2_BUF_FLAG_QUEUED)) {
1802                 hrt_printk("QBUF - buffer %d is already queued\n", i);
1803                 return 0;
1804         }
1805         buf->vidbuf.flags &= ~V4L2_BUF_FLAG_DONE;
1806         queue_add_tail(buf, &hrtdev->capture_list);
1807         buf->vidbuf.flags |= V4L2_BUF_FLAG_QUEUED;
1808         return 1;
1809 }
1810 
1811 /**
1812  * hrt_dequeuebuffer - called by the DQBUF ioctl.
1813  * Returns a buffer from the done queue. 
1814  */ 
1815 int hrt_dequeuebuffer(struct hrt *hrtdev,
1816                       struct v4l2_buffer *buf) 
1817 {
1818         struct stream_buffer *newbuf;
1819         if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1820                 hrt_printk("DQBUF - Wrong buffer type\n");
1821                 return 0;
1822         }
1823         newbuf = queue_del_head(&hrtdev->done_list);
1824         if (newbuf == NULL) {
1825                 hrt_printk("DQBUF - No buffers on done queue\n");
1826                 return 0;
1827         }
1828         newbuf->vidbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
1829         *buf = newbuf->vidbuf;
1830         return 1;
1831 }
1832 
1833 /**
1834  * mmap_stream_buffer_from_offset - get a buffer via the specified offset
1835  */
1836 static struct stream_buffer *mmap_stream_buffer_from_offset(struct hrt *dev,
1837                                                             unsigned long offset)
1838 {
1839         int i;
1840 
1841         offset *= PAGE_SIZE;
1842         for (i = 0; i < MAX_CAPTURE_BUFFERS; ++i)
1843                 if (offset == dev->stream_buf[i].vidbuf.m.offset)
1844                         return &dev->stream_buf[i];
1845         return NULL;
1846 }
1847 
1848 /**
1849  * hrt_vma_open - increments reference count
1850  */
1851 static void hrt_vma_open(struct vm_area_struct *vma)
1852 {
1853         struct video_device *vdev = video_devdata(vma->vm_file);
1854         struct hrt *hrtdev = vdev->priv;
1855         struct stream_buffer *buf;
1856 
1857         if (hrtdev == NULL)
1858                 return;
1859         buf = mmap_stream_buffer_from_offset(hrtdev, vma->vm_pgoff);
1860         if (buf == NULL)
1861                 return;
1862         buf->vma_refcount++;
1863 }
1864 
1865 /**
1866  * hrt_vma_close - decrements reference count of a stream buffer
1867  */
1868 static void hrt_vma_close(struct vm_area_struct *vma)
1869 {
1870         struct video_device *vdev = video_devdata(vma->vm_file);
1871         struct hrt *hrtdev = vdev->priv;
1872         struct stream_buffer *buf;
1873 
1874         if (hrtdev == NULL)
1875                 return;
1876 
1877         buf = mmap_stream_buffer_from_offset(hrtdev, vma->vm_pgoff);
1878         buf->vma_refcount--;
1879         if (buf->vma_refcount > 0) {
1880                 return;
1881         }
1882         if (hrtdev->streaming) {
1883                 hrt_printk(KERN_WARNING "munmap() called while streaming\n");
1884                 hrt_streamoff(hrtdev, buf->vidbuf.type);
1885         }
1886         if (buf->vaddress != NULL) {
1887                 vfree(buf->vaddress);
1888                 buf->vaddress = NULL;
1889         }
1890         buf->vidbuf.flags = 0;
1891 
1892         if (hrtdev->stream_buffers_mapped > 0)
1893                 hrtdev->stream_buffers_mapped--;
1894 }
1895 
1896 /**
1897  * hrt_vma_nopage - calls kvirt_to_pa to convert the vaddress in the 
1898  * stream buffer (indexed via the offset parameter to mmap()) to
1899  * a page number for access from userspace
1900  */
1901 struct page *hrt_vma_nopage(struct vm_area_struct *vma,
1902                                    unsigned long address, int write_access)
1903 {
1904         struct video_device *vdev = video_devdata(vma->vm_file);
1905         struct hrt *hrtdev = vdev->priv;
1906 
1907         struct stream_buffer *buf;
1908         unsigned long offset_into_buf;
1909         struct page *page;
1910 
1911         buf = mmap_stream_buffer_from_offset(hrtdev, vma->vm_pgoff);
1912         if (buf == NULL)
1913                 return 0;
1914         offset_into_buf = address - vma->vm_start;
1915         if (offset_into_buf >= buf->vidbuf.length) {
1916                 hrt_printk("Attempt to read past end of mmap() buffer\n");
1917                 return 0;
1918         }
1919         page = vmalloc_to_page(buf->vaddress + offset_into_buf);
1920         if (page != NULL)
1921                 atomic_inc(&page->count);
1922         return page;
1923 }
1924 
1925 /**
1926  * hrt_vm_ops - vm operations on /dev/video<n>
1927  */
1928 struct vm_operations_struct hrt_vm_ops = {
1929       .open = hrt_vma_open,
1930       .close = hrt_vma_close,
1931       .nopage = hrt_vma_nopage,
1932 };
1933 
1934 /**
1935  * mmap_request_buffers - called by the ioctl REQBUF to request buffers
1936  */
1937 int mmap_request_buffers(struct hrt *dev,
1938                          struct v4l2_requestbuffers *req)
1939 {
1940         int i;
1941         u32 buflen;
1942         u32 type;
1943 
1944         if (req->count < 1)
1945                 req->count = 1;
1946         if (req->count > MAX_CAPTURE_BUFFERS)
1947                 req->count = MAX_CAPTURE_BUFFERS;
1948         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1949 
1950         /*  The buffer length needs to be a multiple of the page size  */
1951         buflen = (dev->framesize + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1952         dprintk("Granting %d buffers\n", req->count);
1953 
1954         /* Now initialize the buffer structures. Don't allocate the
1955            buffers until they're mapped. */
1956         for (i = 0; i < req->count; i++) {
1957                 dev->stream_buf[i].requested = 1;
1958                 dev->stream_buf[i].vidbuf.index = i;
1959                 dev->stream_buf[i].vidbuf.type = type;
1960                 /* offset must be unique for each buffer, and a multiple
1961                    of PAGE_SIZE on 2.4.x  */
1962                 dev->stream_buf[i].vidbuf.m.offset = PAGE_SIZE * (i + 1);
1963                 dev->stream_buf[i].vidbuf.length = buflen;
1964                 dev->stream_buf[i].vidbuf.bytesused = 0;
1965                 dev->stream_buf[i].vidbuf.flags = 0;
1966                 /* XXX: should set timestamp? */
1967                 dev->stream_buf[i].vidbuf.sequence = 0;
1968                 dev->stream_buf[i].vma_refcount = 0;
1969                 dev->stream_buf[i].vaddress = NULL;
1970                 memset(&dev->stream_buf[i].vidbuf.timecode, 0,
1971                        sizeof(struct v4l2_timecode));
1972         }
1973         for (i = req->count; i < MAX_CAPTURE_BUFFERS; i++)
1974                 dev->stream_buf[i].requested = 0;
1975         dev->stream_buffers_requested = req->count;
1976 
1977         return 1;
1978 }
1979 
1980 /** 
1981  * hrt_mmap - called by the mmap() system call on /dev/video<n>
1982  */
1983 int hrt_mmap(struct file *file, struct vm_area_struct *vma)
1984 {
1985         struct video_device *dev = video_devdata(file);
1986         struct hrt *hrtdev = dev->priv;
1987         struct stream_buffer *buf = NULL;
1988 
1989         down(&hrtdev->sem);
1990 
1991         if (vma->vm_pgoff == (HRT_MAGIC_MMAP_OFFSET / PAGE_SIZE)) {
1992                 int ret;
1993                 ret = hrt_direct_mmap(file, vma);
1994                 up(&hrtdev->sem);
1995                 return ret;
1996         }
1997 
1998         buf = mmap_stream_buffer_from_offset(hrtdev, vma->vm_pgoff);
1999 
2000         if (buf == NULL) {
2001                 hrt_printk("mmap() - Invalid offset parameter\n");
2002                 up(&hrtdev->sem);
2003                 return -EINVAL;
2004         }
2005         if (buf->vidbuf.length != vma->vm_end - vma->vm_start) {
2006                 hrt_printk("mmap() - Bad length parameter\n");
2007                 up(&hrtdev->sem);
2008                 return -EINVAL;
2009         }
2010         if (!buf->requested) {
2011                 hrt_printk("mmap() - Buffer is not available for mapping\n");
2012                 up(&hrtdev->sem);
2013                 return -EINVAL;
2014         }
2015         if (buf->vidbuf.flags & V4L2_BUF_FLAG_MAPPED) {
2016                 hrt_printk("mmap() - Buffer is already mapped\n");
2017                 up(&hrtdev->sem);
2018                 return -EINVAL;
2019         }
2020         if (buf->vaddress != NULL)
2021                 vfree(buf->vaddress);
2022 
2023         /*
2024          * Allocate virtual memory. This memory is mapped to the
2025          * buffer in user space.
2026          */
2027         buf->vaddress = vmalloc(buf->vidbuf.length);
2028         if (buf->vaddress == NULL) {
2029                 hrt_printk("Could not allocate mmap() buffer\n");
2030                 up(&hrtdev->sem);
2031                 return -ENODEV;
2032         }
2033 
2034         buf->vidbuf.flags |= V4L2_BUF_FLAG_MAPPED;
2035         hrtdev->stream_buffers_mapped++;
2036         vma->vm_ops = &hrt_vm_ops;
2037         if (vma->vm_ops->open)
2038                 vma->vm_ops->open(vma);
2039 
2040         up(&hrtdev->sem);
2041         return 0;
2042 }
2043 
2044 /**
2045  * hrt_streamon - called by the STREAMON ioctl. 
2046  */ 
2047 int hrt_streamon(struct hrt *hrtdev, __u32 type) 
2048 {
2049         struct stream_buffer *buf;
2050         
2051         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2052                 printk("STREAMON - Wrong buffer type\n");
2053                 return 0;
2054         }
2055 
2056         /* Move any leftover done buffers to the free pool */ 
2057         while ((buf = queue_del_head(&hrtdev->done_list)))
2058                 buf->vidbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
2059 
2060         hrtdev->streaming = 1;
2061         
2062         return 1;
2063 }
2064 
2065 /**
2066  * hrt_streamoff - called by the STREAMOFF ioctl. 
2067  */ 
2068 void hrt_streamoff(struct hrt *hrtdev, __u32 type)
2069 {
2070         int i = 0;
2071         struct stream_buffer *buf;
2072         if (!hrtdev->streaming) {
2073                 hrt_printk("STREAMOFF - Not in streaming mode\n");
2074                 return;
2075         }
2076         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2077                 hrt_printk("STREAMOFF - Wrong buffer type\n");
2078                 return;
2079         }
2080         
2081         hrtdev->streaming = 0;
2082         while ((buf = queue_del_head(&hrtdev->done_list))) {
2083                 buf->vidbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
2084                 i++;
2085         }
2086 }
2087 
2088 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
2089 EXPORT_NO_SYMBOLS;
2090 #endif
2091 
  This page was automatically generated by the LXR engine.