2 * Copyright (C) 1999-2000 by David Brownell <dbrownell@users.sourceforge.net>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * USB driver for Kodak DC-2XX series digital still cameras
23 * The protocol here is the same as the one going over a serial line, but
24 * it uses USB for speed. Set up /dev/kodak, get gphoto (www.gphoto.org),
27 * This should also work for a number of other digital (non-Kodak) cameras,
28 * by adding the vendor and product IDs to the table below. They'll need
29 * to be the sort using USB just as a fast bulk data channel.
35 * 26 August, 1999 -- first release (0.1), works with my DC-240.
36 * The DC-280 (2Mpixel) should also work, but isn't tested.
37 * If you use gphoto, make sure you have the USB updates.
38 * Lives in a 2.3.14 or so Linux kernel, in drivers/usb.
39 * 31 August, 1999 -- minor update to recognize DC-260 and handle
40 * its endpoints being in a different order. Note that as
41 * of gPhoto 0.36pre, the USB updates are integrated.
42 * 12 Oct, 1999 -- handle DC-280 interface class (0xff not 0x0);
43 * added timeouts to bulk_msg calls. Minor updates, docs.
44 * 03 Nov, 1999 -- update for 2.3.25 kernel API changes.
45 * 08 Jan, 2000 .. multiple camera support
46 * 12 Aug, 2000 .. add some real locking, remove an Oops
47 * 10 Oct, 2000 .. usb_device_id table created.
48 * 01 Nov, 2000 .. usb_device_id support added by Adam J. Richter
49 * 08 Apr, 2001 .. Identify version on module load. gb
51 * Thanks to: the folk who've provided USB product IDs, sent in
52 * patches, and shared their successes!
55 #include <linux/config.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/signal.h>
59 #include <linux/errno.h>
60 #include <linux/miscdevice.h>
61 #include <linux/random.h>
62 #include <linux/poll.h>
63 #include <linux/init.h>
64 #include <linux/slab.h>
65 #include <linux/module.h>
66 #include <linux/devfs_fs_kernel.h>
68 #ifdef CONFIG_USB_DEBUG
73 #include <linux/usb.h>
77 extern devfs_handle_t usb_devfs_handle;
82 #define DRIVER_VERSION "v1.0.0"
83 #define DRIVER_AUTHOR "David Brownell, <dbrownell@users.sourceforge.net>"
84 #define DRIVER_DESC "USB Camera Driver for Kodak DC-2xx series cameras"
87 /* current USB framework handles max of 16 USB devices per driver */
88 #define MAX_CAMERAS 16
90 /* USB char devs use USB_MAJOR and from USB_CAMERA_MINOR_BASE up */
91 #define USB_CAMERA_MINOR_BASE 80
94 // XXX remove packet size limit, now that bulk transfers seem fixed
96 /* Application protocol limit is 0x8002; USB has disliked that limit! */
97 #define MAX_PACKET_SIZE 0x2000 /* e.g. image downloading */
99 #define MAX_READ_RETRY 5 /* times to retry reads */
100 #define MAX_WRITE_RETRY 5 /* times to retry writes */
101 #define RETRY_TIMEOUT (HZ) /* sleep between retries */
104 /* table of cameras that work through this driver */
105 static struct usb_device_id camera_table [] = {
106 /* These have the same application level protocol */
107 { USB_DEVICE(0x040a, 0x0120) }, // Kodak DC-240
108 { USB_DEVICE(0x040a, 0x0130) }, // Kodak DC-280
109 { USB_DEVICE(0x040a, 0x0131) }, // Kodak DC-5000
110 { USB_DEVICE(0x040a, 0x0132) }, // Kodak DC-3400
112 /* These have a different application level protocol which
113 * is part of the Flashpoint "DigitaOS". That supports some
114 * non-camera devices, and some non-Kodak cameras.
115 * Use this driver to get USB and "OpenDis" to talk.
117 { USB_DEVICE(0x040a, 0x0100) }, // Kodak DC-220
118 { USB_DEVICE(0x040a, 0x0110) }, // Kodak DC-260
119 { USB_DEVICE(0x040a, 0x0111) }, // Kodak DC-265
120 { USB_DEVICE(0x040a, 0x0112) }, // Kodak DC-290
121 { USB_DEVICE(0xf003, 0x6002) }, // HP PhotoSmart C500
122 { USB_DEVICE(0x03f0, 0x4102) }, // HP PhotoSmart C618
123 { USB_DEVICE(0x0a17, 0x1001) }, // Pentax EI-200
125 /* Other USB devices may well work here too, so long as they
126 * just stick to half duplex bulk packet exchanges. That
127 * means, among other things, no iso or interrupt endpoints.
130 { } /* Terminating entry */
133 MODULE_DEVICE_TABLE (usb, camera_table);
136 struct camera_state {
137 struct usb_device *dev; /* USB device handle */
138 int inEP; /* read endpoint */
139 int outEP; /* write endpoint */
140 const struct usb_device_id *info; /* DC-240, etc */
141 int subminor; /* which minor dev #? */
142 struct semaphore sem; /* locks this struct */
144 /* this is non-null iff the device is open */
145 char *buf; /* buffer for I/O */
147 devfs_handle_t devfs; /* devfs device */
150 wait_queue_head_t wait; /* for timed waits */
153 /* Support multiple cameras, possibly of different types. */
154 static struct camera_state *minor_data [MAX_CAMERAS];
156 /* make this an rwlock if contention becomes an issue */
157 static DECLARE_MUTEX (state_table_mutex);
159 static ssize_t camera_read (struct file *file,
160 char *buf, size_t len, loff_t *ppos)
162 struct camera_state *camera;
166 if (len > MAX_PACKET_SIZE)
169 camera = (struct camera_state *) file->private_data;
176 /* Big reads are common, for image downloading. Smaller ones
177 * are also common (even "directory listing" commands don't
178 * send very much data). We preserve packet boundaries here,
179 * they matter in the application protocol.
181 for (retries = 0; retries < MAX_READ_RETRY; retries++) {
184 if (signal_pending (current)) {
189 retval = usb_bulk_msg (camera->dev,
190 usb_rcvbulkpipe (camera->dev, camera->inEP),
191 camera->buf, len, &count, HZ*10);
193 dbg ("read (%Zd) - 0x%x %d", len, retval, count);
196 if (copy_to_user (buf, camera->buf, count))
202 if (retval != USB_ST_TIMEOUT)
204 interruptible_sleep_on_timeout (&camera->wait, RETRY_TIMEOUT);
206 dbg ("read (%Zd) - retry", len);
212 static ssize_t camera_write (struct file *file,
213 const char *buf, size_t len, loff_t *ppos)
215 struct camera_state *camera;
216 ssize_t bytes_written = 0;
218 if (len > MAX_PACKET_SIZE)
221 camera = (struct camera_state *) file->private_data;
228 /* most writes will be small: simple commands, sometimes with
229 * parameters. putting images (like borders) into the camera
230 * would be the main use of big writes.
233 char *obuf = camera->buf;
234 int maxretry = MAX_WRITE_RETRY;
235 unsigned long copy_size, thistime;
237 /* it's not clear that retrying can do any good ... or that
238 * fragmenting application packets into N writes is correct.
240 thistime = copy_size = len;
241 if (copy_from_user (obuf, buf, copy_size)) {
242 bytes_written = -EFAULT;
249 if (signal_pending (current)) {
251 bytes_written = -EINTR;
255 result = usb_bulk_msg (camera->dev,
256 usb_sndbulkpipe (camera->dev, camera->outEP),
257 obuf, thistime, &count, HZ*10);
260 dbg ("write USB err - %d", result);
265 maxretry = MAX_WRITE_RETRY;
270 if (result == USB_ST_TIMEOUT) { /* NAK - delay a bit */
273 bytes_written = -ETIME;
276 interruptible_sleep_on_timeout (&camera->wait,
281 bytes_written = -EIO;
284 bytes_written += copy_size;
290 dbg ("wrote %Zd", bytes_written);
291 return bytes_written;
294 static int camera_open (struct inode *inode, struct file *file)
296 struct camera_state *camera = NULL;
300 down (&state_table_mutex);
301 subminor = MINOR (inode->i_rdev) - USB_CAMERA_MINOR_BASE;
302 if (subminor < 0 || subminor >= MAX_CAMERAS
303 || !(camera = minor_data [subminor])) {
304 up (&state_table_mutex);
308 up (&state_table_mutex);
315 if (!(camera->buf = (char *) kmalloc (MAX_PACKET_SIZE, GFP_KERNEL))) {
320 dbg ("open #%d", subminor);
322 file->private_data = camera;
328 static int camera_release (struct inode *inode, struct file *file)
330 struct camera_state *camera;
333 camera = (struct camera_state *) file->private_data;
334 down (&state_table_mutex);
341 subminor = camera->subminor;
343 /* If camera was unplugged with open file ... */
345 minor_data [subminor] = NULL;
350 up (&state_table_mutex);
352 dbg ("close #%d", subminor);
357 /* XXX should define some ioctls to expose camera type
358 * to applications ... what USB exposes should suffice.
359 * apps should be able to see the camera type.
361 static /* const */ struct file_operations usb_camera_fops = {
362 /* Uses GCC initializer extension; simpler to maintain */
367 release: camera_release,
373 camera_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *camera_info)
376 struct usb_interface_descriptor *interface;
377 struct usb_endpoint_descriptor *endpoint;
380 struct camera_state *camera = NULL;
383 /* these have one config, one interface */
384 if (dev->descriptor.bNumConfigurations != 1
385 || dev->config[0].bNumInterfaces != 1) {
386 dbg ("Bogus camera config info");
390 /* models differ in how they report themselves */
391 interface = &dev->actconfig->interface[ifnum].altsetting[0];
392 if ((interface->bInterfaceClass != USB_CLASS_PER_INTERFACE
393 && interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC)
394 || interface->bInterfaceSubClass != 0
395 || interface->bInterfaceProtocol != 0
396 || interface->bNumEndpoints != 2
398 dbg ("Bogus camera interface info");
403 /* select "subminor" number (part of a minor number) */
404 down (&state_table_mutex);
405 for (i = 0; i < MAX_CAMERAS; i++) {
409 if (i >= MAX_CAMERAS) {
410 info ("Ignoring additional USB Camera");
414 /* allocate & init camera state */
415 camera = minor_data [i] = kmalloc (sizeof *camera, GFP_KERNEL);
421 init_MUTEX (&camera->sem);
422 camera->info = camera_info;
423 camera->subminor = i;
425 init_waitqueue_head (&camera->wait);
428 /* get input and output endpoints (either order) */
429 endpoint = interface->endpoint;
430 camera->outEP = camera->inEP = -1;
432 ep = endpoint [0].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
433 direction = endpoint [0].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
434 if (direction == USB_DIR_IN)
439 ep = endpoint [1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
440 direction = endpoint [1].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
441 if (direction == USB_DIR_IN)
446 if (camera->outEP == -1 || camera->inEP == -1
447 || endpoint [0].bmAttributes != USB_ENDPOINT_XFER_BULK
448 || endpoint [1].bmAttributes != USB_ENDPOINT_XFER_BULK
450 dbg ("Bogus endpoints");
454 info ("USB Camera #%d connected, major/minor %d/%d", camera->subminor,
455 USB_MAJOR, USB_CAMERA_MINOR_BASE + camera->subminor);
458 usb_inc_dev_use (dev);
460 /* If we have devfs, register the device */
461 sprintf(name, "dc2xx%d", camera->subminor);
462 camera->devfs = devfs_register(usb_devfs_handle, name,
463 DEVFS_FL_DEFAULT, USB_MAJOR,
464 USB_CAMERA_MINOR_BASE + camera->subminor,
465 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
466 S_IWGRP, &usb_camera_fops, NULL);
471 minor_data [camera->subminor] = NULL;
475 up (&state_table_mutex);
479 static void camera_disconnect(struct usb_device *dev, void *ptr)
481 struct camera_state *camera = (struct camera_state *) ptr;
482 int subminor = camera->subminor;
484 down (&state_table_mutex);
487 devfs_unregister(camera->devfs);
489 /* If camera's not opened, we can clean up right away.
490 * Else apps see a disconnect on next I/O; the release cleans.
493 minor_data [subminor] = NULL;
499 info ("USB Camera #%d disconnected", subminor);
500 usb_dec_dev_use (dev);
504 up (&state_table_mutex);
507 static /* const */ struct usb_driver camera_driver = {
510 id_table: camera_table,
512 disconnect: camera_disconnect,
514 fops: &usb_camera_fops,
515 minor: USB_CAMERA_MINOR_BASE
519 int __init usb_dc2xx_init(void)
521 if (usb_register (&camera_driver) < 0)
523 info(DRIVER_VERSION ":" DRIVER_DESC);
527 void __exit usb_dc2xx_cleanup(void)
529 usb_deregister (&camera_driver);
532 module_init (usb_dc2xx_init);
533 module_exit (usb_dc2xx_cleanup);
535 MODULE_AUTHOR( DRIVER_AUTHOR );
536 MODULE_DESCRIPTION( DRIVER_DESC );
537 MODULE_LICENSE("GPL");