355
|
1 /*
|
|
2 * DMX USB driver
|
|
3 *
|
|
4 * Copyright (C) 2004,2006 Erwin Rol (erwin@erwinrol.com)
|
|
5 *
|
|
6 * This driver is based on the usb-skeleton driver;
|
|
7 *
|
|
8 * Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
|
|
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, version 2.
|
|
13 *
|
|
14 * $Id: dmx_usb.c 41 2004-09-14 23:35:25Z erwin $
|
|
15 */
|
|
16
|
|
17 #include <linux/kernel.h>
|
|
18 #include <linux/errno.h>
|
|
19 #include <linux/init.h>
|
|
20 #include <linux/slab.h>
|
|
21 #include <linux/module.h>
|
|
22 #include <linux/smp_lock.h>
|
|
23 #include <linux/completion.h>
|
|
24 #include <asm/uaccess.h>
|
|
25 #include <linux/usb.h>
|
|
26 #include <linux/version.h>
|
|
27
|
|
28 #include "dmx_usb.h"
|
|
29
|
|
30 #ifdef CONFIG_USB_DEBUG
|
|
31 static int debug = 1;
|
|
32 #else
|
|
33 static int debug;
|
|
34 #endif
|
|
35
|
|
36 /* Use our own dbg macro */
|
|
37 #undef dbg
|
|
38 #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
|
|
39
|
|
40
|
|
41 /* Version Information */
|
|
42 #define DRIVER_VERSION "v0.1.20060816"
|
|
43 #define DRIVER_AUTHOR "Erwin Rol, erwin@erwinrol.com"
|
|
44 #define DRIVER_DESC "DMX USB Driver"
|
|
45
|
|
46 /* Module parameters */
|
|
47 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) )
|
|
48 MODULE_PARM(debug, "i");
|
|
49 MODULE_PARM_DESC(debug, "Debug enabled or not");
|
|
50 #else
|
|
51 module_param(debug, bool, S_IRUGO | S_IWUSR);
|
|
52 MODULE_PARM_DESC(debug, "Debug enabled or not");
|
|
53 #endif
|
|
54
|
|
55 static struct usb_device_id dmx_usb_table [] = {
|
|
56 { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0x400, 0xffff) },
|
|
57 { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_ALT_PID, 0x400, 0xffff) },
|
|
58 { } /* Terminating entry */
|
|
59 };
|
|
60
|
|
61 MODULE_DEVICE_TABLE (usb, dmx_usb_table);
|
|
62
|
|
63 /* Get a minor range for your devices from the usb maintainer */
|
|
64 #define DMX_USB_MINOR_BASE 192
|
|
65
|
|
66 /* Structure to hold all of our device specific stuff */
|
|
67 struct dmx_usb_device {
|
|
68 struct usb_device * udev; /* save off the usb device pointer */
|
|
69 struct usb_interface * interface; /* the interface for this device */
|
|
70 unsigned char minor; /* the starting minor number for this device */
|
|
71 unsigned char num_ports; /* the number of ports this device has */
|
|
72 char num_interrupt_in; /* number of interrupt in endpoints we have */
|
|
73 char num_bulk_in; /* number of bulk in endpoints we have */
|
|
74 char num_bulk_out; /* number of bulk out endpoints we have */
|
|
75
|
|
76 unsigned char * bulk_in_buffer; /* the buffer to receive data */
|
|
77 size_t bulk_in_size; /* the size of the receive buffer */
|
|
78 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
|
|
79
|
|
80 unsigned char * bulk_out_buffer; /* the buffer to send data */
|
|
81 size_t bulk_out_size; /* the size of the send buffer */
|
|
82 struct urb * write_urb; /* the urb used to send data */
|
|
83 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
|
|
84 atomic_t write_busy; /* true iff write urb is busy */
|
|
85 struct completion write_finished; /* wait for the write to finish */
|
|
86
|
|
87 int open; /* if the port is open or not */
|
|
88 int present; /* if the device is not disconnected */
|
|
89 struct semaphore sem; /* locks this structure */
|
|
90 };
|
|
91
|
|
92
|
|
93 /* prevent races between open() and disconnect() */
|
|
94 static DECLARE_MUTEX (disconnect_sem);
|
|
95
|
|
96 /* local function prototypes */
|
|
97 //static ssize_t dmx_usb_read (struct file *file, char *buffer, size_t count, loff_t *ppos);
|
|
98 static ssize_t dmx_usb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos);
|
|
99 static int dmx_usb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
|
|
100 static int dmx_usb_open (struct inode *inode, struct file *file);
|
|
101 static int dmx_usb_release (struct inode *inode, struct file *file);
|
|
102
|
|
103 static int dmx_usb_probe (struct usb_interface *interface, const struct usb_device_id *id);
|
|
104 static void dmx_usb_disconnect (struct usb_interface *interface);
|
|
105
|
|
106 static void dmx_usb_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
|
|
107
|
|
108 static struct file_operations dmx_usb_fops = {
|
|
109 /*
|
|
110 * The owner field is part of the module-locking
|
|
111 * mechanism. The idea is that the kernel knows
|
|
112 * which module to increment the use-counter of
|
|
113 * BEFORE it calls the device's open() function.
|
|
114 * This also means that the kernel can decrement
|
|
115 * the use-counter again before calling release()
|
|
116 * or should the open() function fail.
|
|
117 */
|
|
118 .owner = THIS_MODULE,
|
|
119
|
|
120 /* .read = dmx_usb_read, */
|
|
121 .write = dmx_usb_write,
|
|
122 .ioctl = dmx_usb_ioctl,
|
|
123 .open = dmx_usb_open,
|
|
124 .release = dmx_usb_release,
|
|
125 };
|
|
126
|
|
127 /*
|
|
128 * usb class driver info in order to get a minor number from the usb core,
|
|
129 * and to have the device registered with devfs and the driver core
|
|
130 */
|
|
131 static struct usb_class_driver dmx_usb_class = {
|
|
132 .name = "usb/dmx%d",
|
|
133 .fops = &dmx_usb_fops,
|
|
134 .minor_base = DMX_USB_MINOR_BASE,
|
|
135 };
|
|
136
|
|
137 /* usb specific object needed to register this driver with the usb subsystem */
|
|
138 static struct usb_driver dmx_usb_driver = {
|
|
139 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) )
|
|
140 .owner = THIS_MODULE,
|
|
141 #endif
|
|
142 .name = "dmx_usb",
|
|
143 .probe = dmx_usb_probe,
|
|
144 .disconnect = dmx_usb_disconnect,
|
|
145 .id_table = dmx_usb_table,
|
|
146 };
|
|
147
|
|
148
|
|
149 /**
|
|
150 */
|
|
151 static inline void dmx_usb_debug_data (const char *function, int size, const unsigned char *data)
|
|
152 {
|
|
153 int i;
|
|
154
|
|
155 if (!debug)
|
|
156 return;
|
|
157
|
|
158 printk (KERN_DEBUG __FILE__": %s - length = %d, data = ",
|
|
159 function, size);
|
|
160 for (i = 0; i < size; ++i) {
|
|
161 printk ("%.2x ", data[i]);
|
|
162 }
|
|
163 printk ("\n");
|
|
164 }
|
|
165
|
|
166 static __u32 dmx_usb_baud_to_divisor(int baud)
|
|
167 {
|
|
168 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
|
|
169 __u32 divisor;
|
|
170 int divisor3 = 48000000 / 2 / baud; // divisor shifted 3 bits to the left
|
|
171 divisor = divisor3 >> 3;
|
|
172 divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
|
|
173 /* Deal with special cases for highest baud rates. */
|
|
174 if (divisor == 1) divisor = 0; else // 1.0
|
|
175 if (divisor == 0x4001) divisor = 1; // 1.5
|
|
176 return divisor;
|
|
177 }
|
|
178
|
|
179 static int dmx_usb_set_speed(struct dmx_usb_device* dev)
|
|
180 {
|
|
181 char *buf;
|
|
182 __u16 urb_value;
|
|
183 __u16 urb_index;
|
|
184 __u32 urb_index_value;
|
|
185 int rv;
|
|
186
|
|
187 buf = kmalloc(1, GFP_NOIO);
|
|
188 if (!buf)
|
|
189 return -ENOMEM;
|
|
190
|
|
191 urb_index_value = dmx_usb_baud_to_divisor(250000);
|
|
192 urb_value = (__u16)urb_index_value;
|
|
193 urb_index = (__u16)(urb_index_value >> 16);
|
|
194
|
|
195 rv = usb_control_msg(dev->udev,
|
|
196 usb_sndctrlpipe(dev->udev, 0),
|
|
197 FTDI_SIO_SET_BAUDRATE_REQUEST,
|
|
198 FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
|
|
199 urb_value, urb_index,
|
|
200 buf, 0, HZ*10);
|
|
201
|
|
202 kfree(buf);
|
|
203 return rv;
|
|
204 }
|
|
205
|
|
206 static int dmx_usb_setup(struct dmx_usb_device* dev)
|
|
207 {
|
|
208 __u16 urb_value;
|
|
209 char buf[1];
|
|
210
|
|
211 urb_value = FTDI_SIO_SET_DATA_STOP_BITS_2 | FTDI_SIO_SET_DATA_PARITY_NONE;
|
|
212 urb_value |= 8; // number of data bits
|
|
213
|
|
214 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
|
|
215 FTDI_SIO_SET_DATA_REQUEST,
|
|
216 FTDI_SIO_SET_DATA_REQUEST_TYPE,
|
|
217 urb_value , 0,
|
|
218 buf, 0, HZ*10) < 0) {
|
|
219 err("%s FAILED to set databits/stopbits/parity", __FUNCTION__);
|
|
220 }
|
|
221
|
|
222 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
|
|
223 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
|
|
224 FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
|
|
225 0, 0,
|
|
226 buf, 0, HZ*10) < 0) {
|
|
227 err("%s error from disable flowcontrol urb", __FUNCTION__);
|
|
228 }
|
|
229
|
|
230 dmx_usb_set_speed(dev);
|
|
231
|
|
232 return 0;
|
|
233 }
|
|
234
|
|
235 static void dmx_usb_set_break(struct dmx_usb_device* dev, int break_state)
|
|
236 {
|
|
237 __u16 urb_value = FTDI_SIO_SET_DATA_STOP_BITS_2 | FTDI_SIO_SET_DATA_PARITY_NONE | 8;
|
|
238
|
|
239 char buf[2];
|
|
240
|
|
241 if (break_state) {
|
|
242 urb_value |= FTDI_SIO_SET_BREAK;
|
|
243 }
|
|
244
|
|
245 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
|
|
246 FTDI_SIO_SET_DATA_REQUEST,
|
|
247 FTDI_SIO_SET_DATA_REQUEST_TYPE,
|
|
248 urb_value , 0,
|
|
249 buf, 2, HZ*10) < 0) {
|
|
250 err("%s FAILED to enable/disable break state (state was %d)", __FUNCTION__,break_state);
|
|
251 }
|
|
252
|
|
253
|
|
254 dbg("%s break state is %d - urb is %d", __FUNCTION__,break_state, urb_value);
|
|
255 }
|
|
256
|
|
257 /**
|
|
258 */
|
|
259 static inline void dmx_usb_delete (struct dmx_usb_device *dev)
|
|
260 {
|
|
261 kfree (dev->bulk_in_buffer);
|
|
262 usb_buffer_free (dev->udev, dev->bulk_out_size,
|
|
263 dev->bulk_out_buffer,
|
|
264 dev->write_urb->transfer_dma);
|
|
265 usb_free_urb (dev->write_urb);
|
|
266 kfree (dev);
|
|
267 }
|
|
268
|
|
269
|
|
270 /**
|
|
271 */
|
|
272 static int dmx_usb_open (struct inode *inode, struct file *file)
|
|
273 {
|
|
274 struct dmx_usb_device *dev = NULL;
|
|
275 struct usb_interface *interface;
|
|
276 int subminor;
|
|
277 int retval = 0;
|
|
278
|
|
279 dbg("%s", __FUNCTION__);
|
|
280
|
|
281 subminor = iminor(inode);
|
|
282
|
|
283 /* prevent disconnects */
|
|
284 down (&disconnect_sem);
|
|
285
|
|
286 interface = usb_find_interface (&dmx_usb_driver, subminor);
|
|
287 if (!interface) {
|
|
288 err ("%s - error, can't find device for minor %d",
|
|
289 __FUNCTION__, subminor);
|
|
290 retval = -ENODEV;
|
|
291 goto exit_no_device;
|
|
292 }
|
|
293
|
|
294 dev = usb_get_intfdata(interface);
|
|
295 if (!dev) {
|
|
296 retval = -ENODEV;
|
|
297 goto exit_no_device;
|
|
298 }
|
|
299
|
|
300 /* lock this device */
|
|
301 down (&dev->sem);
|
|
302
|
|
303 /* increment our usage count for the driver */
|
|
304 ++dev->open;
|
|
305
|
|
306 /* save our object in the file's private structure */
|
|
307 file->private_data = dev;
|
|
308
|
|
309 /* unlock this device */
|
|
310 up (&dev->sem);
|
|
311
|
|
312 exit_no_device:
|
|
313 up (&disconnect_sem);
|
|
314 return retval;
|
|
315 }
|
|
316
|
|
317
|
|
318 /**
|
|
319 */
|
|
320 static int dmx_usb_release (struct inode *inode, struct file *file)
|
|
321 {
|
|
322 struct dmx_usb_device *dev;
|
|
323 int retval = 0;
|
|
324
|
|
325 dev = (struct dmx_usb_device *)file->private_data;
|
|
326 if (dev == NULL) {
|
|
327 dbg ("%s - object is NULL", __FUNCTION__);
|
|
328 return -ENODEV;
|
|
329 }
|
|
330
|
|
331 dbg("%s - minor %d", __FUNCTION__, dev->minor);
|
|
332
|
|
333 /* lock our device */
|
|
334 down (&dev->sem);
|
|
335
|
|
336 if (dev->open <= 0) {
|
|
337 dbg ("%s - device not opened", __FUNCTION__);
|
|
338 retval = -ENODEV;
|
|
339 goto exit_not_opened;
|
|
340 }
|
|
341
|
|
342 /* wait for any bulk writes that might be going on to finish up */
|
|
343 if (atomic_read (&dev->write_busy))
|
|
344 wait_for_completion (&dev->write_finished);
|
|
345
|
|
346 --dev->open;
|
|
347
|
|
348 if (!dev->present && !dev->open) {
|
|
349 /* the device was unplugged before the file was released */
|
|
350 up (&dev->sem);
|
|
351 dmx_usb_delete (dev);
|
|
352 return 0;
|
|
353 }
|
|
354
|
|
355 exit_not_opened:
|
|
356 up (&dev->sem);
|
|
357
|
|
358 return retval;
|
|
359 }
|
|
360
|
|
361 #if 0
|
|
362
|
|
363 Read is not yet supported
|
|
364
|
|
365 /**
|
|
366 */
|
|
367 static ssize_t dmx_usb_read (struct file *file, char *buffer, size_t count, loff_t *ppos)
|
|
368 {
|
|
369 struct dmx_usb_device *dev;
|
|
370 int retval = 0;
|
|
371 int bytes_read;
|
|
372
|
|
373 dev = (struct dmx_usb_device *)file->private_data;
|
|
374
|
|
375 dbg("%s - minor %d, count = %Zd", __FUNCTION__, dev->minor, count);
|
|
376
|
|
377 /* lock this object */
|
|
378 down (&dev->sem);
|
|
379
|
|
380 /* verify that the device wasn't unplugged */
|
|
381 if (!dev->present) {
|
|
382 up (&dev->sem);
|
|
383 return -ENODEV;
|
|
384 }
|
|
385
|
|
386 /* do a blocking bulk read to get data from the device */
|
|
387 retval = usb_bulk_msg (dev->udev,
|
|
388 usb_rcvbulkpipe (dev->udev,
|
|
389 dev->bulk_in_endpointAddr),
|
|
390 dev->bulk_in_buffer,
|
|
391 min (dev->bulk_in_size, count),
|
|
392 &bytes_read, HZ*10);
|
|
393
|
|
394 /* if the read was successful, copy the data to userspace */
|
|
395 if (!retval) {
|
|
396 if (copy_to_user (buffer, dev->bulk_in_buffer+2, bytes_read-2))
|
|
397 retval = -EFAULT;
|
|
398 else
|
|
399 retval = bytes_read;
|
|
400 }
|
|
401
|
|
402 /* unlock the device */
|
|
403 up (&dev->sem);
|
|
404 return retval;
|
|
405 }
|
|
406
|
|
407 #endif
|
|
408
|
|
409 static __u16 dmx_usb_get_status(struct dmx_usb_device* dev)
|
|
410 {
|
|
411 int retval = 0;
|
|
412 int count = 0;
|
|
413 __u16 buf;
|
|
414
|
|
415 retval = usb_bulk_msg (dev->udev,
|
|
416 usb_rcvbulkpipe (dev->udev, dev->bulk_in_endpointAddr),
|
|
417 &buf, 2, &count, HZ*10);
|
|
418
|
|
419 if (retval)
|
|
420 return 0;
|
|
421
|
|
422 return buf;
|
|
423 }
|
|
424
|
|
425
|
|
426
|
|
427 /**
|
|
428 * dmx_usb_write
|
|
429 *
|
|
430 * A device driver has to decide how to report I/O errors back to the
|
|
431 * user. The safest course is to wait for the transfer to finish before
|
|
432 * returning so that any errors will be reported reliably. dmx_usb_read()
|
|
433 * works like this. But waiting for I/O is slow, so many drivers only
|
|
434 * check for errors during I/O initiation and do not report problems
|
|
435 * that occur during the actual transfer. That's what we will do here.
|
|
436 *
|
|
437 * A driver concerned with maximum I/O throughput would use double-
|
|
438 * buffering: Two urbs would be devoted to write transfers, so that
|
|
439 * one urb could always be active while the other was waiting for the
|
|
440 * user to send more data.
|
|
441 */
|
|
442 static ssize_t dmx_usb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos)
|
|
443 {
|
|
444 struct dmx_usb_device *dev;
|
|
445 ssize_t bytes_written = 0;
|
|
446 int retval = 0;
|
|
447 __u16 stat;
|
|
448
|
|
449 dev = (struct dmx_usb_device *)file->private_data;
|
|
450
|
|
451 dbg("%s - minor %d, count = %Zd", __FUNCTION__, dev->minor, count);
|
|
452
|
|
453 /* lock this object */
|
|
454 down (&dev->sem);
|
|
455
|
|
456 /* verify that the device wasn't unplugged */
|
|
457 if (!dev->present) {
|
|
458 retval = -ENODEV;
|
|
459 goto exit;
|
|
460 }
|
|
461
|
|
462 /* verify that we actually have some data to write */
|
|
463 if (count == 0) {
|
|
464 dbg("%s - write request of 0 bytes", __FUNCTION__);
|
|
465 goto exit;
|
|
466 }
|
|
467
|
|
468 /* wait for a previous write to finish up; we don't use a timeout
|
|
469 * and so a nonresponsive device can delay us indefinitely.
|
|
470 */
|
|
471 if (atomic_read (&dev->write_busy))
|
|
472 wait_for_completion (&dev->write_finished);
|
|
473
|
|
474 /* we can only write as much as our buffer will hold */
|
|
475 bytes_written = min (dev->bulk_out_size, count);
|
|
476
|
|
477 /* copy the data from userspace into our transfer buffer;
|
|
478 * this is the only copy required.
|
|
479 */
|
|
480 if (copy_from_user(dev->write_urb->transfer_buffer, buffer,
|
|
481 bytes_written)) {
|
|
482 retval = -EFAULT;
|
|
483 goto exit;
|
|
484 }
|
|
485
|
|
486 dmx_usb_debug_data (__FUNCTION__, bytes_written,
|
|
487 dev->write_urb->transfer_buffer);
|
|
488
|
|
489 /* this urb was already set up, except for this write size */
|
|
490 dev->write_urb->transfer_buffer_length = bytes_written;
|
|
491
|
|
492 /* Poll the device to see if the transmit buffer is empty */
|
|
493 do {
|
|
494 stat = dmx_usb_get_status(dev);
|
|
495 if (stat == 0) {
|
|
496 retval = -EFAULT;
|
|
497 goto exit;
|
|
498 }
|
|
499 } while ( (stat & ((FTDI_RS_TEMT) << 8) ) == 0 ) ;
|
|
500
|
|
501 /* the transmit buffer is empty, now toggle the break */
|
|
502 dmx_usb_set_break(dev, 1);
|
|
503 dmx_usb_set_break(dev, 0);
|
|
504
|
|
505 /* send the data out the bulk port */
|
|
506 /* a character device write uses GFP_KERNEL,
|
|
507 unless a spinlock is held */
|
|
508 init_completion (&dev->write_finished);
|
|
509 atomic_set (&dev->write_busy, 1);
|
|
510 retval = usb_submit_urb(dev->write_urb, GFP_KERNEL);
|
|
511 if (retval) {
|
|
512 atomic_set (&dev->write_busy, 0);
|
|
513 err("%s - failed submitting write urb, error %d",
|
|
514 __FUNCTION__, retval);
|
|
515 } else {
|
|
516 retval = bytes_written;
|
|
517 }
|
|
518
|
|
519 exit:
|
|
520 /* unlock the device */
|
|
521 up (&dev->sem);
|
|
522
|
|
523 return retval;
|
|
524 }
|
|
525
|
|
526
|
|
527 /**
|
|
528 */
|
|
529 static int dmx_usb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
|
|
530 {
|
|
531 struct dmx_usb_device *dev;
|
|
532
|
|
533 dev = (struct dmx_usb_device *)file->private_data;
|
|
534
|
|
535 /* lock this object */
|
|
536 down (&dev->sem);
|
|
537
|
|
538 /* verify that the device wasn't unplugged */
|
|
539 if (!dev->present) {
|
|
540 up (&dev->sem);
|
|
541 return -ENODEV;
|
|
542 }
|
|
543
|
|
544 dbg("%s - minor %d, cmd 0x%.4x, arg %ld", __FUNCTION__,
|
|
545 dev->minor, cmd, arg);
|
|
546
|
|
547 /* fill in your device specific stuff here */
|
|
548
|
|
549 /* unlock the device */
|
|
550 up (&dev->sem);
|
|
551
|
|
552 /* return that we did not understand this ioctl call */
|
|
553 return -ENOTTY;
|
|
554 }
|
|
555
|
|
556
|
|
557 /**
|
|
558 */
|
|
559 static void dmx_usb_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
|
|
560 {
|
|
561 struct dmx_usb_device *dev = (struct dmx_usb_device *)urb->context;
|
|
562
|
|
563 dbg("%s - minor %d", __FUNCTION__, dev->minor);
|
|
564
|
|
565 /* sync/async unlink faults aren't errors */
|
|
566 if (urb->status && !(urb->status == -ENOENT ||
|
|
567 urb->status == -ECONNRESET)) {
|
|
568 dbg("%s - nonzero write bulk status received: %d",
|
|
569 __FUNCTION__, urb->status);
|
|
570 }
|
|
571
|
|
572 /* notify anyone waiting that the write has finished */
|
|
573 atomic_set (&dev->write_busy, 0);
|
|
574 complete (&dev->write_finished);
|
|
575 }
|
|
576
|
|
577 /**
|
|
578 *
|
|
579 * Called by the usb core when a new device is connected that it thinks
|
|
580 * this driver might be interested in.
|
|
581 */
|
|
582 static int dmx_usb_probe(struct usb_interface *interface, const struct usb_device_id *id)
|
|
583 {
|
|
584 struct usb_device *udev = interface_to_usbdev(interface);
|
|
585 struct dmx_usb_device *dev = NULL;
|
|
586 struct usb_host_interface *iface_desc;
|
|
587 struct usb_endpoint_descriptor *endpoint;
|
|
588 size_t buffer_size;
|
|
589 int i;
|
|
590 int retval = -ENOMEM;
|
|
591
|
|
592 /* See if the device offered us matches what we can accept */
|
|
593 if ((udev->descriptor.idVendor != FTDI_VID) ||
|
|
594 (udev->descriptor.idProduct != FTDI_8U232AM_PID)) {
|
|
595 return -ENODEV;
|
|
596 }
|
|
597
|
|
598 /* allocate memory for our device state and initialize it */
|
|
599 dev = kmalloc (sizeof(struct dmx_usb_device), GFP_KERNEL);
|
|
600 if (dev == NULL) {
|
|
601 err ("Out of memory");
|
|
602 return -ENOMEM;
|
|
603 }
|
|
604 memset (dev, 0x00, sizeof (*dev));
|
|
605
|
|
606 init_MUTEX (&dev->sem);
|
|
607 dev->udev = udev;
|
|
608 dev->interface = interface;
|
|
609
|
|
610 /* set up the endpoint information */
|
|
611 /* check out the endpoints */
|
|
612 /* use only the first bulk-in and bulk-out endpoints */
|
|
613 iface_desc = &interface->altsetting[0];
|
|
614 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
|
|
615 endpoint = &iface_desc->endpoint[i].desc;
|
|
616
|
|
617 if (!dev->bulk_in_endpointAddr &&
|
|
618 (endpoint->bEndpointAddress & USB_DIR_IN) &&
|
|
619 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
|
|
620 == USB_ENDPOINT_XFER_BULK)) {
|
|
621 /* we found a bulk in endpoint */
|
|
622 buffer_size = endpoint->wMaxPacketSize;
|
|
623 dev->bulk_in_size = buffer_size;
|
|
624 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
|
|
625 dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
|
|
626 if (!dev->bulk_in_buffer) {
|
|
627 err("Couldn't allocate bulk_in_buffer");
|
|
628 goto error;
|
|
629 }
|
|
630 }
|
|
631
|
|
632 if (!dev->bulk_out_endpointAddr &&
|
|
633 !(endpoint->bEndpointAddress & USB_DIR_IN) &&
|
|
634 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
|
|
635 == USB_ENDPOINT_XFER_BULK)) {
|
|
636 /* we found a bulk out endpoint */
|
|
637 /* a probe() may sleep and has no restrictions on memory allocations */
|
|
638 dev->write_urb = usb_alloc_urb(0, GFP_KERNEL);
|
|
639 if (!dev->write_urb) {
|
|
640 err("No free urbs available");
|
|
641 goto error;
|
|
642 }
|
|
643 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
|
|
644
|
|
645 /* on some platforms using this kind of buffer alloc
|
|
646 * call eliminates a dma "bounce buffer".
|
|
647 *
|
|
648 * NOTE: you'd normally want i/o buffers that hold
|
|
649 * more than one packet, so that i/o delays between
|
|
650 * packets don't hurt throughput.
|
|
651 */
|
|
652 buffer_size = endpoint->wMaxPacketSize;
|
|
653 dev->bulk_out_size = 513;
|
|
654 dev->write_urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
|
|
655 dev->bulk_out_buffer = usb_buffer_alloc (udev,
|
|
656 buffer_size, GFP_KERNEL,
|
|
657 &dev->write_urb->transfer_dma);
|
|
658 if (!dev->bulk_out_buffer) {
|
|
659 err("Couldn't allocate bulk_out_buffer");
|
|
660 goto error;
|
|
661 }
|
|
662 usb_fill_bulk_urb(dev->write_urb, udev,
|
|
663 usb_sndbulkpipe(udev,
|
|
664 endpoint->bEndpointAddress),
|
|
665 dev->bulk_out_buffer, buffer_size,
|
|
666 dmx_usb_write_bulk_callback, dev);
|
|
667 }
|
|
668 }
|
|
669 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
|
|
670 err("Couldn't find both bulk-in and bulk-out endpoints");
|
|
671 goto error;
|
|
672 }
|
|
673
|
|
674 dmx_usb_setup(dev);
|
|
675
|
|
676 /* allow device read, write and ioctl */
|
|
677 dev->present = 1;
|
|
678
|
|
679 /* we can register the device now, as it is ready */
|
|
680 usb_set_intfdata (interface, dev);
|
|
681 retval = usb_register_dev (interface, &dmx_usb_class);
|
|
682 if (retval) {
|
|
683 /* something prevented us from registering this driver */
|
|
684 err ("Not able to get a minor for this device.");
|
|
685 usb_set_intfdata (interface, NULL);
|
|
686 goto error;
|
|
687 }
|
|
688
|
|
689 dev->minor = interface->minor;
|
|
690
|
|
691 /* let the user know what node this device is now attached to */
|
|
692 info ("DMX USB device now attached to dmx%d", dev->minor);
|
|
693 return 0;
|
|
694
|
|
695 error:
|
|
696 dmx_usb_delete (dev);
|
|
697 return retval;
|
|
698 }
|
|
699
|
|
700
|
|
701 /**
|
|
702 *
|
|
703 * Called by the usb core when the device is removed from the system.
|
|
704 *
|
|
705 * This routine guarantees that the driver will not submit any more urbs
|
|
706 * by clearing dev->udev. It is also supposed to terminate any currently
|
|
707 * active urbs. Unfortunately, usb_bulk_msg(), used in dmx_usb_read(), does
|
|
708 * not provide any way to do this. But at least we can cancel an active
|
|
709 * write.
|
|
710 */
|
|
711 static void dmx_usb_disconnect(struct usb_interface *interface)
|
|
712 {
|
|
713 struct dmx_usb_device *dev;
|
|
714 int minor;
|
|
715
|
|
716 /* prevent races with open() */
|
|
717 down (&disconnect_sem);
|
|
718
|
|
719 dev = usb_get_intfdata (interface);
|
|
720 usb_set_intfdata (interface, NULL);
|
|
721
|
|
722 down (&dev->sem);
|
|
723
|
|
724 minor = dev->minor;
|
|
725
|
|
726 /* give back our minor */
|
|
727 usb_deregister_dev (interface, &dmx_usb_class);
|
|
728
|
|
729 /* terminate an ongoing write */
|
|
730 if (atomic_read (&dev->write_busy)) {
|
|
731 usb_unlink_urb (dev->write_urb);
|
|
732 wait_for_completion (&dev->write_finished);
|
|
733 }
|
|
734
|
|
735 /* prevent device read, write and ioctl */
|
|
736 dev->present = 0;
|
|
737
|
|
738 up (&dev->sem);
|
|
739
|
|
740 /* if the device is opened, dmx_usb_release will clean this up */
|
|
741 if (!dev->open)
|
|
742 dmx_usb_delete (dev);
|
|
743
|
|
744 up (&disconnect_sem);
|
|
745
|
|
746 info("DMX USB #%d now disconnected", minor);
|
|
747 }
|
|
748
|
|
749
|
|
750
|
|
751 /**
|
|
752 * dmx_usb_init
|
|
753 */
|
|
754 static int __init dmx_usb_init(void)
|
|
755 {
|
|
756 int result;
|
|
757
|
|
758 /* register this driver with the USB subsystem */
|
|
759 result = usb_register(&dmx_usb_driver);
|
|
760 if (result) {
|
|
761 err("usb_register failed. Error number %d",
|
|
762 result);
|
|
763 return result;
|
|
764 }
|
|
765
|
|
766 info(DRIVER_DESC " " DRIVER_VERSION);
|
|
767 return 0;
|
|
768 }
|
|
769
|
|
770
|
|
771 /**
|
|
772 * dmx_usb_exit
|
|
773 */
|
|
774 static void __exit dmx_usb_exit(void)
|
|
775 {
|
|
776 /* deregister this driver with the USB subsystem */
|
|
777 usb_deregister(&dmx_usb_driver);
|
|
778 }
|
|
779
|
|
780
|
|
781 module_init (dmx_usb_init);
|
|
782 module_exit (dmx_usb_exit);
|
|
783
|
|
784 MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
785 MODULE_DESCRIPTION(DRIVER_DESC);
|
|
786 MODULE_LICENSE("GPL");
|
|
787
|