~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/rio500.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* -*- linux-c -*- */
  2 
  3 /* 
  4  * Driver for USB Rio 500
  5  *
  6  * Cesar Miquel (miquel@df.uba.ar)
  7  * 
  8  * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
  9  * 
 10  * This program is free software; you can redistribute it and/or
 11  * modify it under the terms of the GNU General Public License as
 12  * published by the Free Software Foundation; either version 2 of the
 13  * License, or (at your option) any later version.
 14  *
 15  * This program is distributed in the hope that it will be useful, but
 16  * WITHOUT ANY WARRANTY; without even the implied warranty of
 17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18  * General Public License for more details.
 19  *
 20  * You should have received a copy of the GNU General Public License
 21  * along with this program; if not, write to the Free Software
 22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 23  *
 24  * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
 25  *
 26  * */
 27 #include <linux/module.h>
 28 #include <linux/kernel.h>
 29 #include <linux/signal.h>
 30 #include <linux/sched.h>
 31 #include <linux/errno.h>
 32 #include <linux/miscdevice.h>
 33 #include <linux/random.h>
 34 #include <linux/poll.h>
 35 #include <linux/init.h>
 36 #include <linux/malloc.h>
 37 #include <linux/spinlock.h>
 38 #include <linux/usb.h>
 39 #include <linux/smp_lock.h>
 40 
 41 #include "rio500_usb.h"
 42 
 43 #define RIO_MINOR   64
 44 
 45 /* stall/wait timeout for rio */
 46 #define NAK_TIMEOUT (HZ)
 47 
 48 #define IBUF_SIZE 0x1000
 49 
 50 /* Size of the rio buffer */
 51 #define OBUF_SIZE 0x10000
 52 
 53 struct rio_usb_data {
 54         struct usb_device *rio_dev;     /* init: probe_rio */
 55         unsigned int ifnum;             /* Interface number of the USB device */
 56         int isopen;                     /* nz if open */
 57         int present;                    /* Device is present on the bus */
 58         char *obuf, *ibuf;              /* transfer buffers */
 59         char bulk_in_ep, bulk_out_ep;   /* Endpoint assignments */
 60         wait_queue_head_t wait_q;       /* for timeouts */
 61         struct semaphore lock;          /* general race avoidance */
 62 };
 63 
 64 static struct rio_usb_data rio_instance;
 65 
 66 static int open_rio(struct inode *inode, struct file *file)
 67 {
 68         struct rio_usb_data *rio = &rio_instance;
 69 
 70         lock_kernel();
 71 
 72         if (rio->isopen || !rio->present) {
 73                 unlock_kernel();
 74                 return -EBUSY;
 75         }
 76         rio->isopen = 1;
 77 
 78         init_waitqueue_head(&rio->wait_q);
 79 
 80         MOD_INC_USE_COUNT;
 81 
 82         unlock_kernel();
 83 
 84         info("Rio opened.");
 85 
 86         return 0;
 87 }
 88 
 89 static int close_rio(struct inode *inode, struct file *file)
 90 {
 91         struct rio_usb_data *rio = &rio_instance;
 92 
 93         rio->isopen = 0;
 94 
 95         MOD_DEC_USE_COUNT;
 96 
 97         info("Rio closed.");
 98         return 0;
 99 }
100 
101 static int
102 ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
103           unsigned long arg)
104 {
105         struct RioCommand rio_cmd;
106         struct rio_usb_data *rio = &rio_instance;
107         void *data;
108         unsigned char *buffer;
109         int result, requesttype;
110         int retries;
111         int retval;
112 
113         /* Sanity check to make sure rio is connected, powered, etc */
114         if ( rio == NULL ||
115              rio->present == 0 ||
116              rio->rio_dev == NULL )
117           return -1;
118 
119         switch (cmd) {
120         case RIO_RECV_COMMAND:
121                 data = (void *) arg;
122                 if (data == NULL)
123                         break;
124                 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
125                         retval = -EFAULT;
126                         goto err_out;
127                 }
128                 if (rio_cmd.length > PAGE_SIZE) {
129                         retval = -EINVAL;
130                         goto err_out;
131                 }
132                 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
133                 if (buffer == NULL)
134                         return -ENOMEM;
135                 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
136                         retval = -EFAULT;
137                         free_page((unsigned long) buffer);
138                         goto err_out;
139                 }
140 
141                 requesttype = rio_cmd.requesttype | USB_DIR_IN |
142                     USB_TYPE_VENDOR | USB_RECIP_DEVICE;
143                 dbg
144                     ("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
145                      requesttype, rio_cmd.request, rio_cmd.value,
146                      rio_cmd.index, rio_cmd.length);
147                 /* Send rio control message */
148                 retries = 3;
149                 down(&(rio->lock));
150                 while (retries) {
151                         result = usb_control_msg(rio->rio_dev,
152                                                  usb_rcvctrlpipe(rio-> rio_dev, 0),
153                                                  rio_cmd.request,
154                                                  requesttype,
155                                                  rio_cmd.value,
156                                                  rio_cmd.index, buffer,
157                                                  rio_cmd.length,
158                                                  rio_cmd.timeout);
159                         if (result == -ETIMEDOUT)
160                                 retries--;
161                         else if (result < 0) {
162                                 err("Error executing ioctrl. code = %d",
163                                      le32_to_cpu(result));
164                                 retries = 0;
165                         } else {
166                                 dbg("Executed ioctl. Result = %d (data=%04x)",
167                                      le32_to_cpu(result),
168                                      le32_to_cpu(*((long *) buffer)));
169                                 if (copy_to_user(rio_cmd.buffer, buffer,
170                                                  rio_cmd.length)) {
171                                         up(&(rio->lock));
172                                         free_page((unsigned long) buffer);
173                                         retval = -EFAULT;
174                                         goto err_out;
175                                 }
176                                 retries = 0;
177                         }
178 
179                         /* rio_cmd.buffer contains a raw stream of single byte
180                            data which has been returned from rio.  Data is
181                            interpreted at application level.  For data that
182                            will be cast to data types longer than 1 byte, data
183                            will be little_endian and will potentially need to
184                            be swapped at the app level */
185 
186                 }
187                 up(&(rio->lock));
188                 free_page((unsigned long) buffer);
189                 break;
190 
191         case RIO_SEND_COMMAND:
192                 data = (void *) arg;
193                 if (data == NULL)
194                         break;
195                 if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand)))
196                         return -EFAULT;
197                 if (rio_cmd.length > PAGE_SIZE)
198                         return -EINVAL;
199                 buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
200                 if (buffer == NULL)
201                         return -ENOMEM;
202                 if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
203                         free_page((unsigned long)buffer);
204                         return -EFAULT;
205                 }
206 
207                 requesttype = rio_cmd.requesttype | USB_DIR_OUT |
208                     USB_TYPE_VENDOR | USB_RECIP_DEVICE;
209                 dbg("sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
210                      requesttype, rio_cmd.request, rio_cmd.value,
211                      rio_cmd.index, rio_cmd.length);
212                 /* Send rio control message */
213                 retries = 3;
214                 down(&(rio->lock));
215                 while (retries) {
216                         result = usb_control_msg(rio->rio_dev,
217                                                  usb_sndctrlpipe(rio-> rio_dev, 0),
218                                                  rio_cmd.request,
219                                                  requesttype,
220                                                  rio_cmd.value,
221                                                  rio_cmd.index, buffer,
222                                                  rio_cmd.length,
223                                                  rio_cmd.timeout);
224                         if (result == -ETIMEDOUT)
225                                 retries--;
226                         else if (result < 0) {
227                                 err("Error executing ioctrl. code = %d",
228                                      le32_to_cpu(result));
229                                 retries = 0;
230                         } else {
231                                 dbg("Executed ioctl. Result = %d",
232                                        le32_to_cpu(result));
233                                 retries = 0;
234 
235                         }
236 
237                 }
238                 up(&(rio->lock));
239                 free_page((unsigned long) buffer);
240                 break;
241 
242         default:
243                 return -ENOIOCTLCMD;
244                 break;
245         }
246 
247         return 0;
248 
249 err_out:
250         return retval;
251 }
252 
253 static ssize_t
254 write_rio(struct file *file, const char *buffer,
255           size_t count, loff_t * ppos)
256 {
257         struct rio_usb_data *rio = &rio_instance;
258 
259         unsigned long copy_size;
260         unsigned long bytes_written = 0;
261         unsigned int partial;
262 
263         int result = 0;
264         int maxretry;
265         int errn = 0;
266 
267         /* Sanity check to make sure rio is connected, powered, etc */
268         if ( rio == NULL ||
269              rio->present == 0 ||
270              rio->rio_dev == NULL )
271           return -1;
272 
273         down(&(rio->lock));
274 
275         do {
276                 unsigned long thistime;
277                 char *obuf = rio->obuf;
278 
279                 thistime = copy_size =
280                     (count >= OBUF_SIZE) ? OBUF_SIZE : count;
281                 if (copy_from_user(rio->obuf, buffer, copy_size)) {
282                         errn = -EFAULT;
283                         goto error;
284                 }
285                 maxretry = 5;
286                 while (thistime) {
287                         if (!rio->rio_dev) {
288                                 errn = -ENODEV;
289                                 goto error;
290                         }
291                         if (signal_pending(current)) {
292                                 up(&(rio->lock));
293                                 return bytes_written ? bytes_written : -EINTR;
294                         }
295 
296                         result = usb_bulk_msg(rio->rio_dev,
297                                          usb_sndbulkpipe(rio->rio_dev, 2),
298                                          obuf, thistime, &partial, 5 * HZ);
299 
300                         dbg("write stats: result:%d thistime:%lu partial:%u",
301                              result, thistime, partial);
302 
303                         if (result == USB_ST_TIMEOUT) { /* NAK - so hold for a while */
304                                 if (!maxretry--) {
305                                         errn = -ETIME;
306                                         goto error;
307                                 }
308                                 interruptible_sleep_on_timeout(&rio-> wait_q, NAK_TIMEOUT);
309                                 continue;
310                         } else if (!result & partial) {
311                                 obuf += partial;
312                                 thistime -= partial;
313                         } else
314                                 break;
315                 };
316                 if (result) {
317                         err("Write Whoops - %x", result);
318                         errn = -EIO;
319                         goto error;
320                 }
321                 bytes_written += copy_size;
322                 count -= copy_size;
323                 buffer += copy_size;
324         } while (count > 0);
325 
326         up(&(rio->lock));
327 
328         return bytes_written ? bytes_written : -EIO;
329 
330 error:
331         up(&(rio->lock));
332         return errn;
333 }
334 
335 static ssize_t
336 read_rio(struct file *file, char *buffer, size_t count, loff_t * ppos)
337 {
338         struct rio_usb_data *rio = &rio_instance;
339         ssize_t read_count;
340         unsigned int partial;
341         int this_read;
342         int result;
343         int maxretry = 10;
344         char *ibuf = rio->ibuf;
345 
346         /* Sanity check to make sure rio is connected, powered, etc */
347         if ( rio == NULL ||
348              rio->present == 0 ||
349              rio->rio_dev == NULL )
350           return -1;
351 
352         read_count = 0;
353 
354         down(&(rio->lock));
355 
356         while (count > 0) {
357                 if (signal_pending(current)) {
358                         up(&(rio->lock));
359                         return read_count ? read_count : -EINTR;
360                 }
361                 if (!rio->rio_dev) {
362                         up(&(rio->lock));
363                         return -ENODEV;
364                 }
365                 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
366 
367                 result = usb_bulk_msg(rio->rio_dev,
368                                       usb_rcvbulkpipe(rio->rio_dev, 1),
369                                       ibuf, this_read, &partial,
370                                       (int) (HZ * 8));
371 
372                 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
373                        result, this_read, partial);
374 
375                 if (partial) {
376                         count = this_read = partial;
377                 } else if (result == USB_ST_TIMEOUT || result == 15) {  /* FIXME: 15 ??? */
378                         if (!maxretry--) {
379                                 up(&(rio->lock));
380                                 err("read_rio: maxretry timeout");
381                                 return -ETIME;
382                         }
383                         interruptible_sleep_on_timeout(&rio->wait_q,
384                                                        NAK_TIMEOUT);
385                         continue;
386                 } else if (result != USB_ST_DATAUNDERRUN) {
387                         up(&(rio->lock));
388                         err("Read Whoops - result:%u partial:%u this_read:%u",
389                              result, partial, this_read);
390                         return -EIO;
391                 } else {
392                         unlock_kernel();
393                         return (0);
394                 }
395 
396                 if (this_read) {
397                         if (copy_to_user(buffer, ibuf, this_read)) {
398                                 up(&(rio->lock));
399                                 return -EFAULT;
400                         }
401                         count -= this_read;
402                         read_count += this_read;
403                         buffer += this_read;
404                 }
405         }
406         up(&(rio->lock));
407         return read_count;
408 }
409 
410 static void *probe_rio(struct usb_device *dev, unsigned int ifnum,
411                        const struct usb_device_id *id)
412 {
413         struct rio_usb_data *rio = &rio_instance;
414 
415         info("USB Rio found at address %d", dev->devnum);
416 
417         rio->present = 1;
418         rio->rio_dev = dev;
419 
420         if (!(rio->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
421                 err("probe_rio: Not enough memory for the output buffer");
422                 return NULL;
423         }
424         dbg("probe_rio: obuf address:%p", rio->obuf);
425 
426         if (!(rio->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
427                 err("probe_rio: Not enough memory for the input buffer");
428                 kfree(rio->obuf);
429                 return NULL;
430         }
431         dbg("probe_rio: ibuf address:%p", rio->ibuf);
432 
433         init_MUTEX(&(rio->lock));
434 
435         return rio;
436 }
437 
438 static void disconnect_rio(struct usb_device *dev, void *ptr)
439 {
440         struct rio_usb_data *rio = (struct rio_usb_data *) ptr;
441 
442         if (rio->isopen) {
443                 rio->isopen = 0;
444                 /* better let it finish - the release will do whats needed */
445                 rio->rio_dev = NULL;
446                 return;
447         }
448         kfree(rio->ibuf);
449         kfree(rio->obuf);
450 
451         info("USB Rio disconnected.");
452 
453         rio->present = 0;
454 }
455 
456 static struct
457 file_operations usb_rio_fops = {
458         read:           read_rio,
459         write:          write_rio,
460         ioctl:          ioctl_rio,
461         open:           open_rio,
462         release:        close_rio,
463 };
464 
465 static struct usb_device_id rio_table [] = {
466         { USB_DEVICE(0x0841, 1) },              /* Rio 500 */
467         { }                                     /* Terminating entry */
468 };
469 
470 MODULE_DEVICE_TABLE (usb, rio_table);
471 
472 static struct usb_driver rio_driver = {
473         name:           "rio500",
474         probe:          probe_rio,
475         disconnect:     disconnect_rio,
476         fops:           &usb_rio_fops,
477         minor:          RIO_MINOR,
478         id_table:       rio_table,
479 };
480 
481 int usb_rio_init(void)
482 {
483         if (usb_register(&rio_driver) < 0)
484                 return -1;
485 
486         info("USB Rio support registered.");
487         return 0;
488 }
489 
490 
491 void usb_rio_cleanup(void)
492 {
493         struct rio_usb_data *rio = &rio_instance;
494 
495         rio->present = 0;
496         usb_deregister(&rio_driver);
497 
498 
499 }
500 
501 module_init(usb_rio_init);
502 module_exit(usb_rio_cleanup);
503 
504 MODULE_AUTHOR("Cesar Miquel <miquel@df.uba.ar>");
505 MODULE_DESCRIPTION("USB Rio 500 driver");
506 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.