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