Linux 2.4.0-test7-pre7
[davej-history.git] / drivers / usb / dc2xx.c
blob67a55ab463e67bf5c353271f7e95fc0229a247b1
1 /*
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
12 * for more details.
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),
25 * and have fun!
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.
33 * HISTORY
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>
62 #undef DEBUG
63 #include <linux/usb.h>
67 /* current USB framework handles max of 16 USB devices per driver */
68 #define MAX_CAMERAS 8
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 */
89 } cameras [] = {
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.
97 */
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 */
122 /* always valid */
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;
137 int retries;
138 int retval = 0;
140 if (len > MAX_PACKET_SIZE)
141 return -EINVAL;
143 camera = (struct camera_state *) file->private_data;
144 down (&camera->sem);
145 if (!camera->dev) {
146 up (&camera->sem);
147 return -ENODEV;
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++) {
156 int count;
158 if (signal_pending (current)) {
159 retval = -EINTR;
160 break;
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);
169 if (!retval) {
170 if (copy_to_user (buf, camera->buf, count))
171 retval = -EFAULT;
172 else
173 retval = count;
174 break;
176 if (retval != USB_ST_TIMEOUT)
177 break;
178 interruptible_sleep_on_timeout (&camera->wait, RETRY_TIMEOUT);
180 dbg ("read (%d) - retry", len);
182 up (&camera->sem);
183 return retval;
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)
193 return -EINVAL;
195 camera = (struct camera_state *) file->private_data;
196 down (&camera->sem);
197 if (!camera->dev) {
198 up (&camera->sem);
199 return -ENODEV;
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.
206 while (len > 0) {
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;
217 break;
219 while (thistime) {
220 int result;
221 int count;
223 if (signal_pending (current)) {
224 if (!bytes_written)
225 bytes_written = -EINTR;
226 goto done;
229 result = usb_bulk_msg (camera->dev,
230 usb_sndbulkpipe (camera->dev, camera->outEP),
231 obuf, thistime, &count, HZ*10);
233 if (result)
234 dbg ("write USB err - %d", result);
236 if (count) {
237 obuf += count;
238 thistime -= count;
239 maxretry = MAX_WRITE_RETRY;
240 continue;
241 } else if (!result)
242 break;
244 if (result == USB_ST_TIMEOUT) { /* NAK - delay a bit */
245 if (!maxretry--) {
246 if (!bytes_written)
247 bytes_written = -ETIME;
248 goto done;
250 interruptible_sleep_on_timeout (&camera->wait,
251 RETRY_TIMEOUT);
252 continue;
254 if (!bytes_written)
255 bytes_written = -EIO;
256 goto done;
258 bytes_written += copy_size;
259 len -= copy_size;
260 buf += copy_size;
262 done:
263 up (&camera->sem);
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;
271 int subminor;
272 int value = 0;
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);
279 return -ENODEV;
281 down (&camera->sem);
282 up (&state_table_mutex);
284 if (camera->buf) {
285 value = -EBUSY;
286 goto done;
289 if (!(camera->buf = (char *) kmalloc (MAX_PACKET_SIZE, GFP_KERNEL))) {
290 value = -ENOMEM;
291 goto done;
294 dbg ("open #%d", subminor);
296 file->private_data = camera;
297 done:
298 up (&camera->sem);
299 return value;
302 static int camera_release (struct inode *inode, struct file *file)
304 struct camera_state *camera;
305 int subminor;
307 camera = (struct camera_state *) file->private_data;
308 down (&state_table_mutex);
309 down (&camera->sem);
311 if (camera->buf) {
312 kfree (camera->buf);
313 camera->buf = 0;
315 subminor = camera->subminor;
317 /* If camera was unplugged with open file ... */
318 if (!camera->dev) {
319 minor_data [subminor] = NULL;
320 kfree (camera);
322 up (&camera->sem);
323 up (&state_table_mutex);
325 dbg ("close #%d", subminor);
327 return 0;
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 */
336 owner: THIS_MODULE,
337 read: camera_read,
338 write: camera_write,
339 open: camera_open,
340 release: camera_release,
345 static void * camera_probe(struct usb_device *dev, unsigned int ifnum)
347 int i;
348 const struct camera *camera_info = NULL;
349 struct usb_interface_descriptor *interface;
350 struct usb_endpoint_descriptor *endpoint;
351 int direction, ep;
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)
357 continue;
358 if (cameras [i].idProduct != dev->descriptor.idProduct)
359 continue;
360 camera_info = &cameras [i];
361 break;
363 if (camera_info == NULL)
364 return 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");
370 return NULL;
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");
382 return NULL;
386 /* select "subminor" number (part of a minor number) */
387 down (&state_table_mutex);
388 for (i = 0; i < MAX_CAMERAS; i++) {
389 if (!minor_data [i])
390 break;
392 if (i >= MAX_CAMERAS) {
393 info ("Ignoring additional USB Camera");
394 up (&state_table_mutex);
395 goto bye;
398 /* allocate & init camera state */
399 camera = minor_data [i] = kmalloc (sizeof *camera, GFP_KERNEL);
400 if (!camera) {
401 err ("no memory!");
402 up (&state_table_mutex);
403 goto bye;
406 init_MUTEX (&camera->sem);
407 camera->info = camera_info;
408 camera->subminor = i;
409 camera->buf = NULL;
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)
420 camera->inEP = ep;
421 else
422 camera->outEP = ep;
424 ep = endpoint [1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
425 direction = endpoint [1].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
426 if (direction == USB_DIR_IN)
427 camera->inEP = ep;
428 else
429 camera->outEP = ep;
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");
436 goto error;
439 info ("USB Camera #%d connected", camera->subminor);
441 camera->dev = dev;
442 usb_inc_dev_use (dev);
443 goto bye;
445 error:
446 minor_data [camera->subminor] = NULL;
447 kfree (camera);
448 camera = NULL;
449 bye:
450 up (&state_table_mutex);
451 return camera;
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);
460 down (&camera->sem);
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.
465 if (!camera->buf) {
466 minor_data [subminor] = NULL;
467 kfree (camera);
468 } else
469 camera->dev = NULL;
471 info ("USB Camera #%d disconnected", subminor);
472 usb_dec_dev_use (dev);
474 up (&camera->sem);
475 up (&state_table_mutex);
478 static /* const */ struct usb_driver camera_driver = {
479 "dc2xx",
480 camera_probe,
481 camera_disconnect,
482 { NULL, NULL },
483 &usb_camera_fops,
484 USB_CAMERA_MINOR_BASE
488 int __init usb_dc2xx_init(void)
490 if (usb_register (&camera_driver) < 0)
491 return -1;
492 return 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);