2 * Copyright (C) 1999-2000 by David Brownell <david-b@pacbell.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
47 * Thanks to: the folk who've provided USB product IDs, sent in
48 * patches, and shared their sucesses!
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/signal.h>
54 #include <linux/errno.h>
55 #include <linux/miscdevice.h>
56 #include <linux/random.h>
57 #include <linux/poll.h>
58 #include <linux/init.h>
59 #include <linux/malloc.h>
60 #include <linux/module.h>
62 #include <linux/usb.h>
66 /* current USB framework handles max of 16 USB devices per driver */
69 /* USB char devs use USB_MAJOR and from USB_CAMERA_MINOR_BASE up */
70 #define USB_CAMERA_MINOR_BASE 80
73 // XXX remove packet size limit, now that bulk transfers seem fixed
75 /* Application protocol limit is 0x8002; USB has disliked that limit! */
76 #define MAX_PACKET_SIZE 0x2000 /* e.g. image downloading */
78 #define MAX_READ_RETRY 5 /* times to retry reads */
79 #define MAX_WRITE_RETRY 5 /* times to retry writes */
80 #define RETRY_TIMEOUT (HZ) /* sleep between retries */
83 /* table of cameras that work through this driver */
84 static const struct camera
{
87 /* plus hooks for camera-specific info if needed */
89 /* These have the same application level protocol */
90 { 0x040a, 0x0120 }, // Kodak DC-240
91 { 0x040a, 0x0130 }, // Kodak DC-280
93 /* These have a different application level protocol which
94 * is part of the Flashpoint "DigitaOS". That supports some
95 * non-camera devices, and some non-Kodak cameras.
97 { 0x040a, 0x0100 }, // Kodak DC-220
98 { 0x040a, 0x0110 }, // Kodak DC-260
99 { 0x040a, 0x0111 }, // Kodak DC-265
100 { 0x040a, 0x0112 }, // Kodak DC-290
101 // { 0x03f0, 0xffff }, // HP PhotoSmart C500
103 /* Other USB devices may well work here too, so long as they
104 * just stick to half duplex bulk packet exchanges. That
105 * means, among other things, no iso or interrupt endpoints.
110 struct camera_state
{
111 struct usb_device
*dev
; /* USB device handle */
112 char inEP
; /* read endpoint */
113 char outEP
; /* write endpoint */
114 const struct camera
*info
; /* DC-240, etc */
115 int subminor
; /* which minor dev #? */
116 int isActive
; /* I/O taking place? */
118 /* this is non-null iff the device is open */
119 char *buf
; /* buffer for I/O */
122 wait_queue_head_t wait
; /* for timed waits */
126 /* Support multiple cameras, possibly of different types. */
127 static struct camera_state
*minor_data
[MAX_CAMERAS
];
130 static ssize_t
camera_read (struct file
*file
,
131 char *buf
, size_t len
, loff_t
*ppos
)
133 struct camera_state
*camera
;
136 if (len
> MAX_PACKET_SIZE
)
139 camera
= (struct camera_state
*) file
->private_data
;
142 if (camera
->isActive
++)
145 /* Big reads are common, for image downloading. Smaller ones
146 * are also common (even "directory listing" commands don't
147 * send very much data). We preserve packet boundaries here,
148 * they matter in the application protocol.
150 for (retries
= 0; retries
< MAX_READ_RETRY
; retries
++) {
154 if (signal_pending (current
)) {
155 camera
->isActive
= 0;
159 camera
->isActive
= 0;
163 result
= usb_bulk_msg (camera
->dev
,
164 usb_rcvbulkpipe (camera
->dev
, camera
->inEP
),
165 camera
->buf
, len
, &count
, HZ
*10);
167 dbg ("read (%d) - 0x%x %d", len
, result
, count
);
170 if (copy_to_user (buf
, camera
->buf
, count
))
172 camera
->isActive
= 0;
175 if (result
!= USB_ST_TIMEOUT
)
177 interruptible_sleep_on_timeout (&camera
->wait
, RETRY_TIMEOUT
);
179 dbg ("read (%d) - retry", len
);
181 camera
->isActive
= 0;
185 static ssize_t
camera_write (struct file
*file
,
186 const char *buf
, size_t len
, loff_t
*ppos
)
188 struct camera_state
*camera
;
189 ssize_t bytes_written
= 0;
191 if (len
> MAX_PACKET_SIZE
)
194 camera
= (struct camera_state
*) file
->private_data
;
197 if (camera
->isActive
++)
200 /* most writes will be small: simple commands, sometimes with
201 * parameters. putting images (like borders) into the camera
202 * would be the main use of big writes.
205 char *obuf
= camera
->buf
;
206 int maxretry
= MAX_WRITE_RETRY
;
207 unsigned long copy_size
, thistime
;
209 /* it's not clear that retrying can do any good ... or that
210 * fragmenting application packets into N writes is correct.
212 thistime
= copy_size
= len
;
213 if (copy_from_user (obuf
, buf
, copy_size
)) {
214 bytes_written
= -EFAULT
;
221 if (signal_pending (current
)) {
223 bytes_written
= -EINTR
;
228 bytes_written
= -ENODEV
;
232 result
= usb_bulk_msg (camera
->dev
,
233 usb_sndbulkpipe (camera
->dev
, camera
->outEP
),
234 obuf
, thistime
, &count
, HZ
*10);
237 dbg ("write USB err - %x", result
);
242 maxretry
= MAX_WRITE_RETRY
;
247 if (result
== USB_ST_TIMEOUT
) { /* NAK - delay a bit */
250 bytes_written
= -ETIME
;
253 interruptible_sleep_on_timeout (&camera
->wait
,
258 bytes_written
= -EIO
;
261 bytes_written
+= copy_size
;
266 camera
->isActive
= 0;
267 dbg ("wrote %d", bytes_written
);
268 return bytes_written
;
271 static int camera_open (struct inode
*inode
, struct file
*file
)
273 struct camera_state
*camera
;
276 subminor
= MINOR (inode
->i_rdev
) - USB_CAMERA_MINOR_BASE
;
277 if (subminor
< 0 || subminor
>= MAX_CAMERAS
278 || !(camera
= minor_data
[subminor
])) {
282 if (!(camera
->buf
= (char *) kmalloc (MAX_PACKET_SIZE
, GFP_KERNEL
))) {
288 /* Keep driver from being unloaded while it's in use */
291 camera
->isActive
= 0;
292 file
->private_data
= camera
;
296 static int camera_release (struct inode
*inode
, struct file
*file
)
298 struct camera_state
*camera
;
300 camera
= (struct camera_state
*) file
->private_data
;
303 /* If camera was unplugged with open file ... */
305 minor_data
[camera
->subminor
] = NULL
;
316 /* XXX should define some ioctls to expose camera type
317 * to applications ... what USB exposes should suffice.
318 * apps should be able to see the camera type.
320 static /* const */ struct file_operations usb_camera_fops
= {
321 /* Uses GCC initializer extension; simpler to maintain */
325 release
: camera_release
,
330 static void * camera_probe(struct usb_device
*dev
, unsigned int ifnum
)
333 const struct camera
*camera_info
= NULL
;
334 struct usb_interface_descriptor
*interface
;
335 struct usb_endpoint_descriptor
*endpoint
;
337 struct camera_state
*camera
;
339 /* Is it a supported camera? */
340 for (i
= 0; i
< sizeof (cameras
) / sizeof (struct camera
); i
++) {
341 if (cameras
[i
].idVendor
!= dev
->descriptor
.idVendor
)
343 if (cameras
[i
].idProduct
!= dev
->descriptor
.idProduct
)
345 camera_info
= &cameras
[i
];
348 if (camera_info
== NULL
)
351 /* these have one config, one interface */
352 if (dev
->descriptor
.bNumConfigurations
!= 1
353 || dev
->config
[0].bNumInterfaces
!= 1) {
354 dbg ("Bogus camera config info");
358 /* models differ in how they report themselves */
359 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
360 if ((interface
->bInterfaceClass
!= USB_CLASS_PER_INTERFACE
361 && interface
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
)
362 || interface
->bInterfaceSubClass
!= 0
363 || interface
->bInterfaceProtocol
!= 0
364 || interface
->bNumEndpoints
!= 2
366 dbg ("Bogus camera interface info");
371 /* select "subminor" number (part of a minor number) */
372 for (i
= 0; i
< MAX_CAMERAS
; i
++) {
376 if (i
>= MAX_CAMERAS
) {
377 info ("Ignoring additional USB Camera");
381 /* allocate & init camera state */
382 camera
= minor_data
[i
] = kmalloc (sizeof *camera
, GFP_KERNEL
);
387 camera
->info
= camera_info
;
388 camera
->subminor
= i
;
389 camera
->isActive
= 0;
391 init_waitqueue_head (&camera
->wait
);
392 info ("USB Camera #%d connected", camera
->subminor
);
395 /* get input and output endpoints (either order) */
396 endpoint
= interface
->endpoint
;
397 camera
->outEP
= camera
->inEP
= -1;
399 ep
= endpoint
[0].bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
400 direction
= endpoint
[0].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
401 if (direction
== USB_DIR_IN
)
406 ep
= endpoint
[1].bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
407 direction
= endpoint
[1].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
408 if (direction
== USB_DIR_IN
)
413 if (camera
->outEP
== -1 || camera
->inEP
== -1
414 || endpoint
[0].bmAttributes
!= USB_ENDPOINT_XFER_BULK
415 || endpoint
[1].bmAttributes
!= USB_ENDPOINT_XFER_BULK
417 dbg ("Bogus endpoints");
422 if (usb_set_configuration (dev
, dev
->config
[0].bConfigurationValue
)) {
423 err ("Failed usb_set_configuration");
431 minor_data
[camera
->subminor
] = NULL
;
436 static void camera_disconnect(struct usb_device
*dev
, void *ptr
)
438 struct camera_state
*camera
= (struct camera_state
*) ptr
;
439 int subminor
= camera
->subminor
;
441 /* If camera's not opened, we can clean up right away.
442 * Else apps see a disconnect on next I/O; the release cleans.
445 minor_data
[subminor
] = NULL
;
450 info ("USB Camera #%d disconnected", subminor
);
453 static /* const */ struct usb_driver camera_driver
= {
459 USB_CAMERA_MINOR_BASE
463 int __init
usb_dc2xx_init(void)
465 if (usb_register (&camera_driver
) < 0)
470 void __exit
usb_dc2xx_cleanup(void)
472 usb_deregister (&camera_driver
);
476 MODULE_AUTHOR("David Brownell, david-b@pacbell.net");
477 MODULE_DESCRIPTION("USB Camera Driver for Kodak DC-2xx series cameras");
479 module_init (usb_dc2xx_init
);
480 module_exit (usb_dc2xx_cleanup
);