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
46 * 12 Aug, 2000 .. add some real locking, remove an Oops
48 * Thanks to: the folk who've provided USB product IDs, sent in
49 * patches, and shared their sucesses!
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/signal.h>
55 #include <linux/errno.h>
56 #include <linux/miscdevice.h>
57 #include <linux/random.h>
58 #include <linux/poll.h>
59 #include <linux/init.h>
60 #include <linux/malloc.h>
61 #include <linux/module.h>
63 #include <linux/usb.h>
67 /* current USB framework handles max of 16 USB devices per driver */
70 /* USB char devs use USB_MAJOR and from USB_CAMERA_MINOR_BASE up */
71 #define USB_CAMERA_MINOR_BASE 80
74 // XXX remove packet size limit, now that bulk transfers seem fixed
76 /* Application protocol limit is 0x8002; USB has disliked that limit! */
77 #define MAX_PACKET_SIZE 0x2000 /* e.g. image downloading */
79 #define MAX_READ_RETRY 5 /* times to retry reads */
80 #define MAX_WRITE_RETRY 5 /* times to retry writes */
81 #define RETRY_TIMEOUT (HZ) /* sleep between retries */
84 /* table of cameras that work through this driver */
85 static const struct camera
{
86 unsigned short idVendor
;
87 unsigned short idProduct
;
88 /* plus hooks for camera-specific info if needed */
90 /* These have the same application level protocol */
91 { 0x040a, 0x0120 }, // Kodak DC-240
92 { 0x040a, 0x0130 }, // Kodak DC-280
94 /* These have a different application level protocol which
95 * is part of the Flashpoint "DigitaOS". That supports some
96 * non-camera devices, and some non-Kodak cameras.
98 { 0x040a, 0x0100 }, // Kodak DC-220
99 { 0x040a, 0x0110 }, // Kodak DC-260
100 { 0x040a, 0x0111 }, // Kodak DC-265
101 { 0x040a, 0x0112 }, // Kodak DC-290
102 { 0xf003, 0x6002 }, // HP PhotoSmart C500
104 /* Other USB devices may well work here too, so long as they
105 * just stick to half duplex bulk packet exchanges. That
106 * means, among other things, no iso or interrupt endpoints.
111 struct camera_state
{
112 struct usb_device
*dev
; /* USB device handle */
113 int inEP
; /* read endpoint */
114 int outEP
; /* write endpoint */
115 const struct camera
*info
; /* DC-240, etc */
116 int subminor
; /* which minor dev #? */
117 struct semaphore sem
; /* locks this struct */
119 /* this is non-null iff the device is open */
120 char *buf
; /* buffer for I/O */
123 wait_queue_head_t wait
; /* for timed waits */
127 /* Support multiple cameras, possibly of different types. */
128 static struct camera_state
*minor_data
[MAX_CAMERAS
];
130 /* make this an rwlock if contention becomes an issue */
131 static DECLARE_MUTEX (state_table_mutex
);
133 static ssize_t
camera_read (struct file
*file
,
134 char *buf
, size_t len
, loff_t
*ppos
)
136 struct camera_state
*camera
;
140 if (len
> MAX_PACKET_SIZE
)
143 camera
= (struct camera_state
*) file
->private_data
;
150 /* Big reads are common, for image downloading. Smaller ones
151 * are also common (even "directory listing" commands don't
152 * send very much data). We preserve packet boundaries here,
153 * they matter in the application protocol.
155 for (retries
= 0; retries
< MAX_READ_RETRY
; retries
++) {
158 if (signal_pending (current
)) {
163 retval
= 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
, retval
, count
);
170 if (copy_to_user (buf
, camera
->buf
, count
))
176 if (retval
!= USB_ST_TIMEOUT
)
178 interruptible_sleep_on_timeout (&camera
->wait
, RETRY_TIMEOUT
);
180 dbg ("read (%d) - retry", len
);
186 static ssize_t
camera_write (struct file
*file
,
187 const char *buf
, size_t len
, loff_t
*ppos
)
189 struct camera_state
*camera
;
190 ssize_t bytes_written
= 0;
192 if (len
> MAX_PACKET_SIZE
)
195 camera
= (struct camera_state
*) file
->private_data
;
202 /* most writes will be small: simple commands, sometimes with
203 * parameters. putting images (like borders) into the camera
204 * would be the main use of big writes.
207 char *obuf
= camera
->buf
;
208 int maxretry
= MAX_WRITE_RETRY
;
209 unsigned long copy_size
, thistime
;
211 /* it's not clear that retrying can do any good ... or that
212 * fragmenting application packets into N writes is correct.
214 thistime
= copy_size
= len
;
215 if (copy_from_user (obuf
, buf
, copy_size
)) {
216 bytes_written
= -EFAULT
;
223 if (signal_pending (current
)) {
225 bytes_written
= -EINTR
;
229 result
= usb_bulk_msg (camera
->dev
,
230 usb_sndbulkpipe (camera
->dev
, camera
->outEP
),
231 obuf
, thistime
, &count
, HZ
*10);
234 dbg ("write USB err - %d", result
);
239 maxretry
= MAX_WRITE_RETRY
;
244 if (result
== USB_ST_TIMEOUT
) { /* NAK - delay a bit */
247 bytes_written
= -ETIME
;
250 interruptible_sleep_on_timeout (&camera
->wait
,
255 bytes_written
= -EIO
;
258 bytes_written
+= copy_size
;
264 dbg ("wrote %d", bytes_written
);
265 return bytes_written
;
268 static int camera_open (struct inode
*inode
, struct file
*file
)
270 struct camera_state
*camera
= NULL
;
274 down (&state_table_mutex
);
275 subminor
= MINOR (inode
->i_rdev
) - USB_CAMERA_MINOR_BASE
;
276 if (subminor
< 0 || subminor
>= MAX_CAMERAS
277 || !(camera
= minor_data
[subminor
])) {
278 up (&state_table_mutex
);
282 up (&state_table_mutex
);
289 if (!(camera
->buf
= (char *) kmalloc (MAX_PACKET_SIZE
, GFP_KERNEL
))) {
294 dbg ("open #%d", subminor
);
296 file
->private_data
= camera
;
302 static int camera_release (struct inode
*inode
, struct file
*file
)
304 struct camera_state
*camera
;
307 camera
= (struct camera_state
*) file
->private_data
;
308 down (&state_table_mutex
);
315 subminor
= camera
->subminor
;
317 /* If camera was unplugged with open file ... */
319 minor_data
[subminor
] = NULL
;
323 up (&state_table_mutex
);
325 dbg ("close #%d", subminor
);
330 /* XXX should define some ioctls to expose camera type
331 * to applications ... what USB exposes should suffice.
332 * apps should be able to see the camera type.
334 static /* const */ struct file_operations usb_camera_fops
= {
335 /* Uses GCC initializer extension; simpler to maintain */
340 release
: camera_release
,
345 static void * camera_probe(struct usb_device
*dev
, unsigned int ifnum
)
348 const struct camera
*camera_info
= NULL
;
349 struct usb_interface_descriptor
*interface
;
350 struct usb_endpoint_descriptor
*endpoint
;
352 struct camera_state
*camera
= NULL
;
354 /* Is it a supported camera? */
355 for (i
= 0; i
< sizeof (cameras
) / sizeof (struct camera
); i
++) {
356 if (cameras
[i
].idVendor
!= dev
->descriptor
.idVendor
)
358 if (cameras
[i
].idProduct
!= dev
->descriptor
.idProduct
)
360 camera_info
= &cameras
[i
];
363 if (camera_info
== NULL
)
366 /* these have one config, one interface */
367 if (dev
->descriptor
.bNumConfigurations
!= 1
368 || dev
->config
[0].bNumInterfaces
!= 1) {
369 dbg ("Bogus camera config info");
373 /* models differ in how they report themselves */
374 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
375 if ((interface
->bInterfaceClass
!= USB_CLASS_PER_INTERFACE
376 && interface
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
)
377 || interface
->bInterfaceSubClass
!= 0
378 || interface
->bInterfaceProtocol
!= 0
379 || interface
->bNumEndpoints
!= 2
381 dbg ("Bogus camera interface info");
386 /* select "subminor" number (part of a minor number) */
387 down (&state_table_mutex
);
388 for (i
= 0; i
< MAX_CAMERAS
; i
++) {
392 if (i
>= MAX_CAMERAS
) {
393 info ("Ignoring additional USB Camera");
394 up (&state_table_mutex
);
398 /* allocate & init camera state */
399 camera
= minor_data
[i
] = kmalloc (sizeof *camera
, GFP_KERNEL
);
402 up (&state_table_mutex
);
406 init_MUTEX (&camera
->sem
);
407 camera
->info
= camera_info
;
408 camera
->subminor
= i
;
410 init_waitqueue_head (&camera
->wait
);
413 /* get input and output endpoints (either order) */
414 endpoint
= interface
->endpoint
;
415 camera
->outEP
= camera
->inEP
= -1;
417 ep
= endpoint
[0].bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
418 direction
= endpoint
[0].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
419 if (direction
== USB_DIR_IN
)
424 ep
= endpoint
[1].bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
425 direction
= endpoint
[1].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
;
426 if (direction
== USB_DIR_IN
)
431 if (camera
->outEP
== -1 || camera
->inEP
== -1
432 || endpoint
[0].bmAttributes
!= USB_ENDPOINT_XFER_BULK
433 || endpoint
[1].bmAttributes
!= USB_ENDPOINT_XFER_BULK
435 dbg ("Bogus endpoints");
439 info ("USB Camera #%d connected", camera
->subminor
);
442 usb_inc_dev_use (dev
);
446 minor_data
[camera
->subminor
] = NULL
;
450 up (&state_table_mutex
);
454 static void camera_disconnect(struct usb_device
*dev
, void *ptr
)
456 struct camera_state
*camera
= (struct camera_state
*) ptr
;
457 int subminor
= camera
->subminor
;
459 down (&state_table_mutex
);
462 /* If camera's not opened, we can clean up right away.
463 * Else apps see a disconnect on next I/O; the release cleans.
466 minor_data
[subminor
] = NULL
;
471 info ("USB Camera #%d disconnected", subminor
);
472 usb_dec_dev_use (dev
);
475 up (&state_table_mutex
);
478 static /* const */ struct usb_driver camera_driver
= {
484 USB_CAMERA_MINOR_BASE
488 int __init
usb_dc2xx_init(void)
490 if (usb_register (&camera_driver
) < 0)
495 void __exit
usb_dc2xx_cleanup(void)
497 usb_deregister (&camera_driver
);
501 MODULE_AUTHOR("David Brownell, david-b@pacbell.net");
502 MODULE_DESCRIPTION("USB Camera Driver for Kodak DC-2xx series cameras");
504 module_init (usb_dc2xx_init
);
505 module_exit (usb_dc2xx_cleanup
);