Line 0
Link Here
|
|
|
1 |
/* |
2 |
* USB Microsoft IR Transceiver driver - 0.1 |
3 |
* |
4 |
* Copyright (c) 2003-2004 Dan Conti (dconti@acm.wwu.edu) |
5 |
* |
6 |
* This driver is based on the USB skeleton driver packaged with the |
7 |
* kernel, and the notice from that package has been retained below. |
8 |
* |
9 |
* The Microsoft IR Transceiver is a neat little IR receiver with two |
10 |
* emitters on it designed for Windows Media Center. This driver might |
11 |
* work for all media center remotes, but I have only tested it with |
12 |
* the philips model. The first revision of this driver only supports |
13 |
* the receive function - the transmit function will be much more |
14 |
* tricky due to the nature of the hardware. Microsoft chose to build |
15 |
* this device inexpensively, therefore making it extra dumb. There |
16 |
* is no interrupt endpoint on this device; all usb traffic happens |
17 |
* over two bulk endpoints. As a result of this, poll() for this |
18 |
* device is an actual hardware poll (instead of a receive queue |
19 |
* check) and is rather expensive. |
20 |
* |
21 |
* This driver is structured in three basic layers |
22 |
* - lower - interface with the usb device and manage usb data |
23 |
* - middle - api to convert usb data into mode2 and provide this in |
24 |
* _read calls |
25 |
* - mceusb_* - linux driver interface |
26 |
* |
27 |
* The key routines are as follows: |
28 |
* msir_fetch_more_data - this reads incoming data, strips off the |
29 |
* start codes the ir receiver places on them, |
30 |
* and dumps it in an * internal buffer |
31 |
* msir_generate_mode2 - this takes the above data, depacketizes it, |
32 |
* and generates mode2 data to feed out |
33 |
* through read calls |
34 |
* |
35 |
* |
36 |
* All trademarks property of their respective owners. |
37 |
* |
38 |
* 2003_11_11 - Restructured to minimalize code interpretation in the |
39 |
* driver. The normal use case will be with lirc. |
40 |
* |
41 |
* 2004_01_01 - Removed all code interpretation. Generate mode2 data |
42 |
* for passing off to lirc. Cleanup |
43 |
* |
44 |
* 2004_01_04 - Removed devfs handle. Put in a temporary workaround |
45 |
* for a known issue where repeats generate two |
46 |
* sequential spaces * (last_was_repeat_gap) |
47 |
* |
48 |
* TODO |
49 |
* - Fix up minor number, registration of major/minor with usb subsystem |
50 |
* - Fix up random EINTR being sent |
51 |
* - Fix problem where third key in a repeat sequence is randomly truncated |
52 |
* |
53 |
*/ |
54 |
/* |
55 |
* USB Skeleton driver - 0.6 |
56 |
* |
57 |
* Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com) |
58 |
* |
59 |
* This program is free software; you can redistribute it and/or |
60 |
* modify it under the terms of the GNU General Public License as |
61 |
* published by the Free Software Foundation; either version 2 of |
62 |
* the License, or (at your option) any later version. |
63 |
* |
64 |
* |
65 |
* This driver is to be used as a skeleton driver to be able to create a |
66 |
* USB driver quickly. The design of it is based on the usb-serial and |
67 |
* dc2xx drivers. |
68 |
* |
69 |
* Thanks to Oliver Neukum and David Brownell for their help in debugging |
70 |
* this driver. |
71 |
* |
72 |
* TODO: |
73 |
* - fix urb->status race condition in write sequence |
74 |
* - move minor_table to a dynamic list. |
75 |
* |
76 |
* History: |
77 |
* |
78 |
* 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect. |
79 |
* Thanks to Pete Zaitcev for the fix. |
80 |
* 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux |
81 |
* 2001_08_21 - 0.4 - more small bug fixes. |
82 |
* 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel |
83 |
* 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people |
84 |
* 2001_05_01 - 0.1 - first version |
85 |
* |
86 |
*/ |
87 |
|
88 |
#include <linux/config.h> |
89 |
#include <linux/kernel.h> |
90 |
#include <linux/sched.h> |
91 |
#include <linux/wait.h> |
92 |
#include <linux/signal.h> |
93 |
#include <linux/errno.h> |
94 |
#include <linux/poll.h> |
95 |
#include <linux/init.h> |
96 |
#include <linux/slab.h> |
97 |
#include <linux/fcntl.h> |
98 |
#include <linux/module.h> |
99 |
#include <linux/spinlock.h> |
100 |
#include <linux/list.h> |
101 |
#include <linux/smp_lock.h> |
102 |
#include <linux/usb.h> |
103 |
|
104 |
#ifdef CONFIG_USB_DEBUG |
105 |
static int debug = 1; |
106 |
#else |
107 |
static int debug = 1; |
108 |
#endif |
109 |
|
110 |
#include <linux/lirc.h> |
111 |
#include "drivers/lirc_dev/lirc_dev.h" |
112 |
|
113 |
/* Version Information */ |
114 |
#define DRIVER_VERSION "v0.1" |
115 |
#define DRIVER_AUTHOR "Dan Conti, dconti@acm.wwu.edu" |
116 |
#define DRIVER_DESC "USB Microsoft IR Transceiver Driver" |
117 |
|
118 |
/* Module paramaters */ |
119 |
MODULE_PARM(debug, "i"); |
120 |
MODULE_PARM_DESC(debug, "Debug enabled or not"); |
121 |
|
122 |
/* Define these values to match your device */ |
123 |
#define USB_MCEUSB_VENDOR_ID 0x045e |
124 |
#define USB_MCEUSB_PRODUCT_ID 0x006d |
125 |
|
126 |
/* table of devices that work with this driver */ |
127 |
static struct usb_device_id mceusb_table [] = { |
128 |
{ USB_DEVICE(USB_MCEUSB_VENDOR_ID, USB_MCEUSB_PRODUCT_ID) }, |
129 |
{ } /* Terminating entry */ |
130 |
}; |
131 |
|
132 |
MODULE_DEVICE_TABLE (usb, mceusb_table); |
133 |
|
134 |
/* XXX TODO, 244 is likely unused but not reserved */ |
135 |
/* Get a minor range for your devices from the usb maintainer */ |
136 |
#define USB_MCEUSB_MINOR_BASE 244 |
137 |
|
138 |
|
139 |
/* we can have up to this number of device plugged in at once */ |
140 |
#define MAX_DEVICES 16 |
141 |
|
142 |
/* Structure to hold all of our device specific stuff */ |
143 |
struct usb_skel { |
144 |
/* save off the usb device pointer */ |
145 |
struct usb_device * udev; |
146 |
/* the interface for this device */ |
147 |
struct usb_interface * interface; |
148 |
/* the starting minor number for this device */ |
149 |
unsigned char minor; |
150 |
/* the number of ports this device has */ |
151 |
unsigned char num_ports; |
152 |
/* number of interrupt in endpoints we have */ |
153 |
char num_interrupt_in; |
154 |
/* number of bulk in endpoints we have */ |
155 |
char num_bulk_in; |
156 |
/* number of bulk out endpoints we have */ |
157 |
char num_bulk_out; |
158 |
|
159 |
/* the buffer to receive data */ |
160 |
unsigned char * bulk_in_buffer; |
161 |
/* the size of the receive buffer */ |
162 |
int bulk_in_size; |
163 |
/* the address of the bulk in endpoint */ |
164 |
__u8 bulk_in_endpointAddr; |
165 |
|
166 |
/* the buffer to send data */ |
167 |
unsigned char * bulk_out_buffer; |
168 |
/* the size of the send buffer */ |
169 |
int bulk_out_size; |
170 |
/* the urb used to send data */ |
171 |
struct urb * write_urb; |
172 |
/* the address of the bulk out endpoint */ |
173 |
__u8 bulk_out_endpointAddr; |
174 |
|
175 |
wait_queue_head_t wait_q; /* for timeouts */ |
176 |
int open_count; /* number of times this port |
177 |
* has been opened */ |
178 |
struct semaphore sem; /* locks this structure */ |
179 |
|
180 |
struct lirc_plugin* plugin; |
181 |
|
182 |
/* Used in converting to mode2 and storing */ |
183 |
/* buffer for the mode2 data, since lirc reads 4bytes */ |
184 |
int mode2_data[256]; |
185 |
int mode2_idx; /* read index */ |
186 |
int mode2_count; /* words available (i.e. write |
187 |
* index) */ |
188 |
int mode2_partial_pkt_size; |
189 |
int mode2_once; |
190 |
|
191 |
/* Used for storing preprocessed usb data before converting to mode2*/ |
192 |
char usb_dbuffer[1024]; |
193 |
int usb_dstart; |
194 |
int usb_dcount; |
195 |
int usb_valid_bytes_in_bulk_buffer; |
196 |
|
197 |
/* Set to 1 if the last value we adjusted was a repeat gap; we |
198 |
* need to hold this value around until we process a lead |
199 |
* space on the repeat code, otherwise we pass off two |
200 |
* sequential spaces */ |
201 |
int last_was_repeat_gap; |
202 |
}; |
203 |
|
204 |
/* driver api */ |
205 |
static ssize_t mceusb_read (struct file *file, char *buffer, |
206 |
size_t count, loff_t *ppos); |
207 |
static ssize_t mceusb_write (struct file *file, const char *buffer, |
208 |
size_t count, loff_t *ppos); |
209 |
static unsigned int mceusb_poll (struct file* file, poll_table* wait); |
210 |
|
211 |
static int mceusb_open (struct inode *inode, struct file *file); |
212 |
static int mceusb_release (struct inode *inode, struct file *file); |
213 |
|
214 |
static void * mceusb_probe (struct usb_device *dev, unsigned int ifnum, |
215 |
const struct usb_device_id *id); |
216 |
static void *mceusb_disconnect (struct usb_interface *intf); |
217 |
|
218 |
static void mceusb_write_bulk_callback (struct urb *urb); |
219 |
|
220 |
/* lower level api */ |
221 |
static int msir_fetch_more_data( struct usb_skel* dev, int dont_block ); |
222 |
static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len ); |
223 |
static int msir_mark_as_read( struct usb_skel* dev, int count ); |
224 |
static int msir_available_data( struct usb_skel* dev ); |
225 |
|
226 |
/* middle */ |
227 |
static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data, |
228 |
int bytecount ); |
229 |
static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count ); |
230 |
static int msir_available_mode2( struct usb_skel* dev ); |
231 |
|
232 |
/* helper functions */ |
233 |
static void msir_cleanup( struct usb_skel* dev ); |
234 |
static int set_use_inc(void* data); |
235 |
static void set_use_dec(void* data); |
236 |
|
237 |
/* array of pointers to our devices that are currently connected */ |
238 |
static struct usb_skel *minor_table[MAX_DEVICES]; |
239 |
|
240 |
/* lock to protect the minor_table structure */ |
241 |
static DECLARE_MUTEX (minor_table_mutex); |
242 |
|
243 |
/* |
244 |
* File operations needed when we register this driver. |
245 |
* This assumes that this driver NEEDS file operations, |
246 |
* of course, which means that the driver is expected |
247 |
* to have a node in the /dev directory. If the USB |
248 |
* device were for a network interface then the driver |
249 |
* would use "struct net_driver" instead, and a serial |
250 |
* device would use "struct tty_driver". |
251 |
*/ |
252 |
static struct file_operations mceusb_fops = { |
253 |
/* |
254 |
* The owner field is part of the module-locking |
255 |
* mechanism. The idea is that the kernel knows |
256 |
* which module to increment the use-counter of |
257 |
* BEFORE it calls the device's open() function. |
258 |
* This also means that the kernel can decrement |
259 |
* the use-counter again before calling release() |
260 |
* or should the open() function fail. |
261 |
* |
262 |
* Not all device structures have an "owner" field |
263 |
* yet. "struct file_operations" and "struct net_device" |
264 |
* do, while "struct tty_driver" does not. If the struct |
265 |
* has an "owner" field, then initialize it to the value |
266 |
* THIS_MODULE and the kernel will handle all module |
267 |
* locking for you automatically. Otherwise, you must |
268 |
* increment the use-counter in the open() function |
269 |
* and decrement it again in the release() function |
270 |
* yourself. |
271 |
*/ |
272 |
owner: THIS_MODULE, |
273 |
|
274 |
read: mceusb_read, |
275 |
write: mceusb_write, |
276 |
poll: mceusb_poll, |
277 |
ioctl: NULL, |
278 |
open: mceusb_open, |
279 |
release: mceusb_release, |
280 |
}; |
281 |
|
282 |
|
283 |
/* usb specific object needed to register this driver with the usb subsystem */ |
284 |
static struct usb_driver mceusb_driver = { |
285 |
name: "ir_transceiver", |
286 |
probe: mceusb_probe, |
287 |
disconnect: mceusb_disconnect, |
288 |
fops: &mceusb_fops, |
289 |
minor: USB_MCEUSB_MINOR_BASE, |
290 |
id_table: mceusb_table, |
291 |
}; |
292 |
|
293 |
|
294 |
/** |
295 |
* usb_mceusb_debug_data |
296 |
*/ |
297 |
static inline void usb_mceusb_debug_data (const char *function, int size, |
298 |
const unsigned char *data) |
299 |
{ |
300 |
int i; |
301 |
|
302 |
if (!debug) |
303 |
return; |
304 |
|
305 |
printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", |
306 |
function, size); |
307 |
for (i = 0; i < size; ++i) { |
308 |
printk ("%.2x ", data[i]); |
309 |
} |
310 |
printk ("\n"); |
311 |
} |
312 |
|
313 |
|
314 |
/** |
315 |
* mceusb_delete |
316 |
*/ |
317 |
static inline void mceusb_delete (struct usb_skel *dev) |
318 |
{ |
319 |
minor_table[dev->minor] = NULL; |
320 |
if (dev->bulk_in_buffer != NULL) |
321 |
kfree (dev->bulk_in_buffer); |
322 |
if (dev->bulk_out_buffer != NULL) |
323 |
kfree (dev->bulk_out_buffer); |
324 |
if (dev->write_urb != NULL) |
325 |
usb_free_urb (dev->write_urb); |
326 |
kfree (dev); |
327 |
} |
328 |
|
329 |
static void mceusb_setup( struct usb_device *udev ) |
330 |
{ |
331 |
char data[8]; |
332 |
int res; |
333 |
memset( data, 0, 8 ); |
334 |
|
335 |
/* Get Status */ |
336 |
res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
337 |
USB_REQ_GET_STATUS, USB_DIR_IN, |
338 |
0, 0, data, 2, HZ * 3); |
339 |
|
340 |
/* res = usb_get_status( udev, 0, 0, data ); */ |
341 |
dbg(__FUNCTION__ " res = %d status = 0x%x 0x%x", |
342 |
res, data[0], data[1] ); |
343 |
|
344 |
/* This is a strange one. They issue a set address to the |
345 |
* device on the receive control pipe and expect a certain |
346 |
* value pair back |
347 |
*/ |
348 |
memset( data, 0, 8 ); |
349 |
|
350 |
res = usb_control_msg( udev, usb_rcvctrlpipe(udev, 0), |
351 |
5, USB_TYPE_VENDOR, 0, 0, |
352 |
data, 2, HZ * 3 ); |
353 |
dbg(__FUNCTION__ " res = %d, devnum = %d", res, udev->devnum); |
354 |
dbg(__FUNCTION__ " data[0] = %d, data[1] = %d", data[0], data[1] ); |
355 |
|
356 |
/* set feature */ |
357 |
res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0), |
358 |
USB_REQ_SET_FEATURE, USB_TYPE_VENDOR, |
359 |
0xc04e, 0x0000, NULL, 0, HZ * 3 ); |
360 |
|
361 |
dbg(__FUNCTION__ " res = %d", res); |
362 |
|
363 |
/* These two are sent by the windows driver, but stall for |
364 |
* me. I dont have an analyzer on the linux side so i can't |
365 |
* see what is actually different and why * the device takes |
366 |
* issue with them |
367 |
*/ |
368 |
#if 0 |
369 |
/* this is some custom control message they send */ |
370 |
res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0), |
371 |
0x04, USB_TYPE_VENDOR, |
372 |
0x0808, 0x0000, NULL, 0, HZ * 3 ); |
373 |
|
374 |
dbg(__FUNCTION__ " res = %d", res); |
375 |
|
376 |
/* this is another custom control message they send */ |
377 |
res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0), |
378 |
0x02, USB_TYPE_VENDOR, |
379 |
0x0000, 0x0100, NULL, 0, HZ * 3 ); |
380 |
|
381 |
dbg(__FUNCTION__ " res = %d", res); |
382 |
#endif |
383 |
} |
384 |
|
385 |
/** |
386 |
* mceusb_open |
387 |
*/ |
388 |
static int mceusb_open (struct inode *inode, struct file *file) |
389 |
{ |
390 |
struct usb_skel *dev = NULL; |
391 |
struct usb_device* udev = NULL; |
392 |
int subminor; |
393 |
int retval = 0; |
394 |
|
395 |
dbg(__FUNCTION__); |
396 |
|
397 |
/* This is a very sucky point. On lirc, we get passed the |
398 |
* minor number of the lirc device, which is totally |
399 |
* retarded. We want to support people opening /dev/usb/msir0 |
400 |
* directly though, so try and determine who the hell is |
401 |
* calling us here |
402 |
*/ |
403 |
if( MAJOR( inode->i_rdev ) != USB_MAJOR ) |
404 |
{ |
405 |
/* This is the lirc device just passing on the |
406 |
* request. We probably mismatch minor numbers here, |
407 |
* but the lucky fact is that nobody will ever use two |
408 |
* of the exact same remotes with two recievers on one |
409 |
* machine |
410 |
*/ |
411 |
subminor = 0; |
412 |
} else { |
413 |
subminor = MINOR (inode->i_rdev) - USB_MCEUSB_MINOR_BASE; |
414 |
} |
415 |
if ((subminor < 0) || |
416 |
(subminor >= MAX_DEVICES)) { |
417 |
dbg("subminor %d", subminor); |
418 |
return -ENODEV; |
419 |
} |
420 |
|
421 |
/* Increment our usage count for the module. |
422 |
* This is redundant here, because "struct file_operations" |
423 |
* has an "owner" field. This line is included here soley as |
424 |
* a reference for drivers using lesser structures... ;-) |
425 |
*/ |
426 |
try_module_get(THIS_MODULE); |
427 |
|
428 |
/* lock our minor table and get our local data for this minor */ |
429 |
down (&minor_table_mutex); |
430 |
dev = minor_table[subminor]; |
431 |
if (dev == NULL) { |
432 |
dbg("dev == NULL"); |
433 |
up (&minor_table_mutex); |
434 |
module_put(THIS_MODULE); |
435 |
return -ENODEV; |
436 |
} |
437 |
udev = dev->udev; |
438 |
|
439 |
/* lock this device */ |
440 |
down (&dev->sem); |
441 |
|
442 |
/* unlock the minor table */ |
443 |
up (&minor_table_mutex); |
444 |
|
445 |
/* increment our usage count for the driver */ |
446 |
++dev->open_count; |
447 |
|
448 |
/* save our object in the file's private structure */ |
449 |
file->private_data = dev; |
450 |
|
451 |
/* init the waitq */ |
452 |
init_waitqueue_head( &dev->wait_q ); |
453 |
|
454 |
/* clear off the first few messages. these look like |
455 |
* calibration or test data, i can't really tell |
456 |
* this also flushes in case we have random ir data queued up |
457 |
*/ |
458 |
{ |
459 |
char junk[64]; |
460 |
int partial = 0, retval, i; |
461 |
for( i = 0; i < 40; i++ ) |
462 |
{ |
463 |
retval = usb_bulk_msg (udev, |
464 |
usb_rcvbulkpipe |
465 |
(udev, |
466 |
dev->bulk_in_endpointAddr), |
467 |
junk, 64, |
468 |
&partial, HZ*10); |
469 |
} |
470 |
} |
471 |
|
472 |
msir_cleanup( dev ); |
473 |
|
474 |
/* unlock this device */ |
475 |
up (&dev->sem); |
476 |
|
477 |
return retval; |
478 |
} |
479 |
|
480 |
|
481 |
/** |
482 |
* mceusb_release |
483 |
*/ |
484 |
static int mceusb_release (struct inode *inode, struct file *file) |
485 |
{ |
486 |
struct usb_skel *dev; |
487 |
int retval = 0; |
488 |
|
489 |
dev = (struct usb_skel *)file->private_data; |
490 |
if (dev == NULL) { |
491 |
dbg (__FUNCTION__ " - object is NULL"); |
492 |
return -ENODEV; |
493 |
} |
494 |
|
495 |
dbg(__FUNCTION__ " - minor %d", dev->minor); |
496 |
|
497 |
/* lock our minor table */ |
498 |
down (&minor_table_mutex); |
499 |
|
500 |
/* lock our device */ |
501 |
down (&dev->sem); |
502 |
|
503 |
if (dev->open_count <= 0) { |
504 |
dbg (__FUNCTION__ " - device not opened"); |
505 |
retval = -ENODEV; |
506 |
goto exit_not_opened; |
507 |
} |
508 |
|
509 |
if (dev->udev == NULL) { |
510 |
/* the device was unplugged before the file was released */ |
511 |
up (&dev->sem); |
512 |
mceusb_delete (dev); |
513 |
up (&minor_table_mutex); |
514 |
module_put(THIS_MODULE); |
515 |
return 0; |
516 |
} |
517 |
|
518 |
/* decrement our usage count for the device */ |
519 |
--dev->open_count; |
520 |
if (dev->open_count <= 0) { |
521 |
/* shutdown any bulk writes that might be going on */ |
522 |
usb_unlink_urb (dev->write_urb); |
523 |
dev->open_count = 0; |
524 |
} |
525 |
|
526 |
/* decrement our usage count for the module */ |
527 |
module_put(THIS_MODULE); |
528 |
|
529 |
exit_not_opened: |
530 |
up (&dev->sem); |
531 |
up (&minor_table_mutex); |
532 |
|
533 |
return retval; |
534 |
} |
535 |
|
536 |
static void msir_cleanup( struct usb_skel* dev ) |
537 |
{ |
538 |
memset( dev->bulk_in_buffer, 0, dev->bulk_in_size ); |
539 |
|
540 |
memset( dev->usb_dbuffer, 0, sizeof(dev->usb_dbuffer) ); |
541 |
dev->usb_dstart = 0; |
542 |
dev->usb_dcount = 0; |
543 |
dev->usb_valid_bytes_in_bulk_buffer = 0; |
544 |
|
545 |
memset( dev->mode2_data, 0, sizeof(dev->mode2_data) ); |
546 |
dev->mode2_partial_pkt_size = 0; |
547 |
dev->mode2_count = 0; |
548 |
dev->mode2_idx = 0; |
549 |
dev->mode2_once = 0; |
550 |
dev->last_was_repeat_gap = 0; |
551 |
} |
552 |
|
553 |
static int set_use_inc(void* data) |
554 |
{ |
555 |
/* struct usb_skel* skel = (struct usb_skel*)data; */ |
556 |
|
557 |
try_module_get(THIS_MODULE); |
558 |
return 0; |
559 |
} |
560 |
|
561 |
static void set_use_dec(void* data) |
562 |
{ |
563 |
/* check for unplug here */ |
564 |
struct usb_skel* dev = (struct usb_skel*) data; |
565 |
if( !dev->udev ) |
566 |
{ |
567 |
lirc_unregister_plugin( dev->minor ); |
568 |
lirc_buffer_free( dev->plugin->rbuf ); |
569 |
kfree( dev->plugin->rbuf ); |
570 |
kfree( dev->plugin ); |
571 |
} |
572 |
|
573 |
module_put(THIS_MODULE); |
574 |
} |
575 |
|
576 |
static int msir_available_mode2( struct usb_skel* dev ) |
577 |
{ |
578 |
return dev->mode2_count - dev->last_was_repeat_gap; |
579 |
} |
580 |
|
581 |
static int msir_available_data( struct usb_skel* dev ) |
582 |
{ |
583 |
return dev->usb_dcount; |
584 |
} |
585 |
|
586 |
static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count ) |
587 |
{ |
588 |
int words_to_read = count; |
589 |
// int words_avail = dev->mode2_count; |
590 |
int words_avail = msir_available_mode2( dev ); |
591 |
|
592 |
if( !dev->mode2_once && words_avail ) |
593 |
{ |
594 |
int space = PULSE_MASK; |
595 |
count--; |
596 |
copy_to_user( mode2_data, &space, 4 ); |
597 |
dev->mode2_once = 1; |
598 |
|
599 |
if( count ) |
600 |
{ |
601 |
mode2_data++; |
602 |
} |
603 |
else |
604 |
{ |
605 |
return 1; |
606 |
} |
607 |
} |
608 |
|
609 |
if( !words_avail ) |
610 |
{ |
611 |
return 0; |
612 |
} |
613 |
|
614 |
if( words_to_read > words_avail ) |
615 |
{ |
616 |
words_to_read = words_avail; |
617 |
} |
618 |
|
619 |
dbg(__FUNCTION__ " dev->mode2_count %d, dev->mode2_idx %d", |
620 |
dev->mode2_count, dev->mode2_idx); |
621 |
dbg(__FUNCTION__ " words_avail %d words_to_read %d", |
622 |
words_avail, words_to_read); |
623 |
copy_to_user( mode2_data, &( dev->mode2_data[dev->mode2_idx] ), |
624 |
words_to_read<<2 ); |
625 |
dbg(__FUNCTION__ " would copy_to_user() %d w", words_to_read); |
626 |
|
627 |
dev->mode2_idx += words_to_read; |
628 |
dev->mode2_count -= words_to_read; |
629 |
|
630 |
if( dev->mode2_count == 0 ) |
631 |
{ |
632 |
dev->mode2_idx = 0; |
633 |
} |
634 |
else if( dev->mode2_count == 1 && dev->last_was_repeat_gap ) |
635 |
{ |
636 |
// shift down the repeat gap and map it up to a |
637 |
// lirc-acceptable value |
638 |
dev->mode2_data[0] = dev->mode2_data[dev->mode2_idx]; |
639 |
if( dev->mode2_data[0] >= 60000 && |
640 |
dev->mode2_data[0] <= 70000 ) |
641 |
dev->mode2_data[0] = 95000; |
642 |
//printk(__FUNCTION__ " shifting value %d down from %d prev %d\n", dev->mode2_data[0], dev->mode2_idx, |
643 |
// dev->mode2_data[dev->mode2_idx-1]); |
644 |
dev->mode2_idx = 0; |
645 |
} |
646 |
|
647 |
return words_to_read; |
648 |
} |
649 |
|
650 |
static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len ) |
651 |
{ |
652 |
if( len > dev->usb_dcount ) |
653 |
{ |
654 |
len = dev->usb_dcount; |
655 |
} |
656 |
memcpy( buffer, dev->usb_dbuffer + dev->usb_dstart, len ); |
657 |
return len; |
658 |
} |
659 |
|
660 |
static int msir_mark_as_read( struct usb_skel* dev, int count ) |
661 |
{ |
662 |
// if( count != dev->usb_dcount ) |
663 |
// printk(KERN_INFO __FUNCTION__ " count %d dev->usb_dcount %d dev->usb_dstart %d", count, dev->usb_dcount, dev->usb_dstart ); |
664 |
if( count > dev->usb_dcount ) |
665 |
count = dev->usb_dcount; |
666 |
dev->usb_dcount -= count; |
667 |
dev->usb_dstart += count; |
668 |
|
669 |
if( !dev->usb_dcount ) |
670 |
dev->usb_dstart = 0; |
671 |
|
672 |
return 0; |
673 |
} |
674 |
|
675 |
|
676 |
/* |
677 |
* msir_fetch_more_data |
678 |
* |
679 |
* The goal here is to read in more remote codes from the remote. In |
680 |
* the event that the remote isn't sending us anything, the caller |
681 |
* will block until a key is pressed (i.e. this performs phys read, |
682 |
* filtering, and queueing of data) unless dont_block is set to 1; in |
683 |
* this situation, it will perform a few reads and will exit out if it |
684 |
* does not see any appropriate data |
685 |
* |
686 |
* dev->sem should be locked when this function is called - fine grain |
687 |
* locking isn't really important here anyways |
688 |
* |
689 |
* TODO change this to do partials based on term codes, or not always fill |
690 |
*/ |
691 |
|
692 |
static int msir_fetch_more_data( struct usb_skel* dev, int dont_block ) |
693 |
{ |
694 |
int retries = 0; |
695 |
int count, this_read, partial; |
696 |
int retval; |
697 |
int writeindex, terminators = 0; |
698 |
int bytes_to_read = sizeof(dev->usb_dbuffer) - dev->usb_dcount; |
699 |
signed char* ibuf; |
700 |
int sequential_empty_reads = 0; |
701 |
|
702 |
/* special case where we are already full */ |
703 |
if( bytes_to_read == 0 ) |
704 |
return dev->usb_dcount; |
705 |
|
706 |
/* shift down */ |
707 |
if( dev->usb_dcount && dev->usb_dstart != 0 ) |
708 |
{ |
709 |
printk( __FUNCTION__ " shifting %d bytes from %d\n", |
710 |
dev->usb_dcount, dev->usb_dstart ); |
711 |
memcpy( dev->usb_dbuffer, dev->usb_dbuffer + dev->usb_dstart, |
712 |
dev->usb_dcount ); |
713 |
} |
714 |
|
715 |
dev->usb_dstart = 0; |
716 |
|
717 |
writeindex = dev->usb_dcount; |
718 |
|
719 |
count = bytes_to_read; |
720 |
|
721 |
ibuf = (signed char*)dev->bulk_in_buffer; |
722 |
if( !dev->usb_valid_bytes_in_bulk_buffer ) |
723 |
{ |
724 |
memset( ibuf, 0, dev->bulk_in_size ); |
725 |
} |
726 |
|
727 |
#if 0 |
728 |
printk( __FUNCTION__ " going to read, dev->usb_dcount %d, bytes_to_read %d vbb %d\n", dev->usb_dcount, bytes_to_read, |
729 |
dev->usb_valid_bytes_in_bulk_buffer ); |
730 |
#endif |
731 |
/* 8 is the minimum read size */ |
732 |
while( count > 8 ) |
733 |
{ |
734 |
int i, goodbytes = 0; |
735 |
|
736 |
/* break out if we were interrupted */ |
737 |
if( signal_pending(current) ) |
738 |
{ |
739 |
printk( __FUNCTION__ " got signal %ld\n", |
740 |
current->pending.signal.sig[0]); |
741 |
return dev->usb_dcount ? dev->usb_dcount : -EINTR; |
742 |
} |
743 |
|
744 |
/* or if we were unplugged */ |
745 |
if( !dev->udev ) |
746 |
{ |
747 |
return -ENODEV; |
748 |
} |
749 |
|
750 |
/* or on data issues */ |
751 |
if( writeindex == sizeof(dev->usb_dbuffer) ) |
752 |
{ |
753 |
printk( __FUNCTION__ " buffer full, returning\n"); |
754 |
return dev->usb_dcount; |
755 |
} |
756 |
|
757 |
// always read the maximum |
758 |
this_read = dev->bulk_in_size; |
759 |
|
760 |
partial = 0; |
761 |
|
762 |
if( dev->usb_valid_bytes_in_bulk_buffer ) { |
763 |
retval = 0; |
764 |
this_read = partial = dev->usb_valid_bytes_in_bulk_buffer; |
765 |
dev->usb_valid_bytes_in_bulk_buffer = 0; |
766 |
} else { |
767 |
// This call always returns almost immediately |
768 |
// with data, since this device will always |
769 |
// provide a 2 byte response on a bulk |
770 |
// read. Not exactly friendly to the usb bus |
771 |
// or our load avg. We attempt to compensate |
772 |
// for this on 2 byte reads below |
773 |
|
774 |
memset( ibuf, 0, dev->bulk_in_size ); |
775 |
retval = usb_bulk_msg (dev->udev, |
776 |
usb_rcvbulkpipe |
777 |
(dev->udev, |
778 |
dev->bulk_in_endpointAddr), |
779 |
(unsigned char*)ibuf, this_read, |
780 |
&partial, HZ*10); |
781 |
} |
782 |
|
783 |
if( retval ) |
784 |
{ |
785 |
/* break out on errors */ |
786 |
printk(__FUNCTION__ " got retval %d %d %d", |
787 |
retval, this_read, partial ); |
788 |
if( retval == USB_ST_DATAOVERRUN && retries < 5 ) |
789 |
{ |
790 |
retries++; |
791 |
interruptible_sleep_on_timeout |
792 |
( &dev->wait_q, HZ ); |
793 |
continue; |
794 |
} |
795 |
else |
796 |
{ |
797 |
return -EIO; |
798 |
} |
799 |
} else { |
800 |
retries = 0; |
801 |
} |
802 |
|
803 |
if( partial ) |
804 |
{ |
805 |
this_read = partial; |
806 |
} |
807 |
|
808 |
/* All packets i've seen start with b1 60. If no data |
809 |
* was actually available, the transceiver still gives |
810 |
* this byte pair back. We only care about actual |
811 |
* codes, so we can safely ignore these 2 byte reads |
812 |
*/ |
813 |
if( this_read > 2 ) |
814 |
{ |
815 |
#if 0 |
816 |
printk( __FUNCTION__ " read %d bytes partial %d goodbytes %d writeidx %d\n", |
817 |
this_read, partial, goodbytes, writeindex ); |
818 |
#endif |
819 |
sequential_empty_reads = 0; |
820 |
/* copy from the input buffer to the capture buffer */ |
821 |
for( i = 0; i < this_read; i++ ) |
822 |
{ |
823 |
if( (((unsigned char*)ibuf)[i] == 0xb1) || |
824 |
(ibuf[i] == 0x60) ) |
825 |
; |
826 |
else |
827 |
{ |
828 |
if( writeindex == sizeof(dev->usb_dbuffer) ) |
829 |
{ |
830 |
/* this can happen in |
831 |
* repeats, where |
832 |
* basically the bulk |
833 |
* buffer is getting |
834 |
* spammed and we |
835 |
* aren't processing |
836 |
* data fast enough |
837 |
*/ |
838 |
#if 1 |
839 |
dev->usb_valid_bytes_in_bulk_buffer = this_read - i; |
840 |
memcpy( ibuf, &( ibuf[i] ), |
841 |
dev->usb_valid_bytes_in_bulk_buffer ); |
842 |
#endif |
843 |
break; |
844 |
} |
845 |
dev->usb_dbuffer[writeindex++] = ibuf[i]; |
846 |
goodbytes++; |
847 |
|
848 |
if( ibuf[i] == 0x7f ) |
849 |
{ |
850 |
terminators++; |
851 |
|
852 |
/* This is a bug - we should either get 10 or 15 */ |
853 |
if( terminators > 15 ) |
854 |
{ |
855 |
dbg("bugbug - terminators %d at %d gb %d", terminators, i, goodbytes ); |
856 |
} else |
857 |
dbg("terminator %d at %d gb %d", terminators, i, goodbytes ); |
858 |
dbg("writeindex %d", writeindex); |
859 |
} |
860 |
else if( terminators ) |
861 |
{ |
862 |
if( ((unsigned char*)ibuf)[i] == 128 ) |
863 |
{ |
864 |
/* copy back any remainder and break out */ |
865 |
dev->usb_valid_bytes_in_bulk_buffer = this_read - (i + 1); |
866 |
if( dev->usb_valid_bytes_in_bulk_buffer ) |
867 |
{ |
868 |
memcpy( ibuf, &( ibuf[i+1] ), dev->usb_valid_bytes_in_bulk_buffer ); |
869 |
} |
870 |
|
871 |
count = 0; |
872 |
break; |
873 |
} |
874 |
if( terminators == 10 || |
875 |
terminators == 15 ) |
876 |
dbg("post-termination data %d idx %d %d", ibuf[i], dev->usb_dcount, i); |
877 |
} |
878 |
} |
879 |
} |
880 |
dev->usb_dcount += goodbytes; |
881 |
count -= goodbytes; |
882 |
} else { |
883 |
sequential_empty_reads++; |
884 |
|
885 |
// assume no data |
886 |
if( dont_block && sequential_empty_reads == 5 ) |
887 |
break; |
888 |
|
889 |
// Try to be nice to the usb bus by sleeping |
890 |
// for a bit here before going in to the next |
891 |
// read |
892 |
interruptible_sleep_on_timeout( &dev->wait_q, 1 ); |
893 |
} |
894 |
|
895 |
} |
896 |
/* return the number of bytes available now */ |
897 |
return dev->usb_dcount; |
898 |
} |
899 |
|
900 |
// layout of data, per Christoph Bartelmus |
901 |
// The protocol is: |
902 |
// 1 byte: -length of following packet |
903 |
// the following bytes of the packet are: |
904 |
// negative value: |
905 |
// -(number of time units) of pulse |
906 |
// positive value: |
907 |
// (number of time units) of space |
908 |
// one time unit is 50us |
909 |
|
910 |
#define MCE_TIME_UNIT 50 |
911 |
|
912 |
// returns the number of bytes processed from the 'usb_data' array |
913 |
static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data, |
914 |
int bytecount ) |
915 |
{ |
916 |
int bytes_left_in_packet = 0; |
917 |
int pos = 0; |
918 |
int mode2count = 0; |
919 |
int last_was_pulse = 1; |
920 |
int last_pkt = 0; |
921 |
int split_pkt_size = 0; |
922 |
// XXX no bounds checking here |
923 |
int* mode2_data; |
924 |
int mode2_limit = sizeof( dev->mode2_data ) - dev->mode2_count; |
925 |
|
926 |
// If data exists in the buffer, we have to point to the last |
927 |
// item there so we can append consecutive pulse/space |
928 |
// ops. Otherwise, set last_was_pulse 1 (since the first byte |
929 |
// is a pulse, and we want to store in the first array |
930 |
// location |
931 |
if( dev->mode2_count == 0 ) |
932 |
{ |
933 |
mode2_data = &( dev->mode2_data[0] ); |
934 |
last_was_pulse = (dev->mode2_once ? 1 : 0); |
935 |
mode2_data[0] = 0; |
936 |
} |
937 |
else |
938 |
{ |
939 |
mode2_data = &( dev->mode2_data[dev->mode2_idx + |
940 |
dev->mode2_count - 1] ); |
941 |
last_was_pulse = (mode2_data[0] & PULSE_BIT) ? 1 : 0; |
942 |
} |
943 |
|
944 |
while( pos < bytecount && !last_pkt && |
945 |
(mode2_limit > (dev->mode2_count + mode2count)) ) |
946 |
{ |
947 |
if( dev->mode2_partial_pkt_size ) |
948 |
{ |
949 |
bytes_left_in_packet = dev->mode2_partial_pkt_size; |
950 |
dev->mode2_partial_pkt_size = 0; |
951 |
} |
952 |
else { |
953 |
bytes_left_in_packet = 128 + usb_data[pos]; |
954 |
|
955 |
// XXX out of sync? find the next packet |
956 |
// header, establish a distance, and fix the |
957 |
// packet size |
958 |
if( bytes_left_in_packet > 4 ) |
959 |
{ |
960 |
int i; |
961 |
for( i = pos + 1; i < pos + 4; i++ ) |
962 |
{ |
963 |
if( (int)(128 + usb_data[i]) <= 4 ) |
964 |
{ |
965 |
bytes_left_in_packet = i - pos; |
966 |
break; |
967 |
} |
968 |
} |
969 |
} |
970 |
else |
971 |
{ |
972 |
// otherwise, increment past the header |
973 |
pos++; |
974 |
} |
975 |
} |
976 |
|
977 |
// special case where we have a terminator at the |
978 |
// start but not at the end of this packet, indicating |
979 |
// potential repeat, or the packet is less than 4 |
980 |
// bytes, indicating end also special case a split |
981 |
// starting packet |
982 |
if( pos > 1 && bytes_left_in_packet < 4 ) |
983 |
{ |
984 |
// end |
985 |
last_pkt = 1; |
986 |
} |
987 |
else if( usb_data[pos] == 127 && |
988 |
usb_data[pos+bytes_left_in_packet-1] != 127 ) |
989 |
{ |
990 |
// the genius ir transciever is blending data |
991 |
// from the repeat events into a single |
992 |
// packet. how we handle this is by splitting |
993 |
// the packet (and truncating the packet size |
994 |
// value we read), then rewriting a new packet |
995 |
// header onto the outbound data. it's |
996 |
// ultraghetto. |
997 |
while( usb_data[pos+bytes_left_in_packet-1] != 127 ) |
998 |
{ |
999 |
bytes_left_in_packet--; |
1000 |
split_pkt_size++; |
1001 |
} |
1002 |
// repeat code |
1003 |
last_pkt = 2; |
1004 |
} |
1005 |
while( bytes_left_in_packet && pos < bytecount ) |
1006 |
{ |
1007 |
int keycode = usb_data[pos]; |
1008 |
int pulse = 0; |
1009 |
|
1010 |
pos++; |
1011 |
if( keycode < 0 ) |
1012 |
{ |
1013 |
pulse = 1; |
1014 |
keycode += 128; |
1015 |
} |
1016 |
keycode *= MCE_TIME_UNIT; |
1017 |
|
1018 |
// on a state change, increment the position |
1019 |
// for the output buffer and initialize the |
1020 |
// current spot to 0; otherwise we need to |
1021 |
// concatenate pulse/gap values for lirc to be |
1022 |
// happy |
1023 |
if( pulse != last_was_pulse && |
1024 |
(mode2count || mode2_data[mode2count])) |
1025 |
{ |
1026 |
if( dev->last_was_repeat_gap ) |
1027 |
{ |
1028 |
//printk( __FUNCTION__ " transition with lwrg set lastval %d idx1 %d idx2 %d\n", |
1029 |
// mode2_data[mode2count],mode2count, dev->mode2_count+dev->mode2_idx-1 ); |
1030 |
} |
1031 |
mode2count++; |
1032 |
mode2_data[mode2count] = 0; |
1033 |
} |
1034 |
|
1035 |
mode2_data[mode2count] += keycode; |
1036 |
|
1037 |
// Or in the pulse bit, and map all gap |
1038 |
// lengths to a fixed value; this makes lirc |
1039 |
// happy, sort of. |
1040 |
if( pulse ) { |
1041 |
mode2_data[mode2count] |= PULSE_BIT; |
1042 |
dev->last_was_repeat_gap = 0; |
1043 |
} |
1044 |
|
1045 |
last_was_pulse = pulse; |
1046 |
bytes_left_in_packet--; |
1047 |
} |
1048 |
} |
1049 |
|
1050 |
// If the last value in the data array is a repeat gap, set |
1051 |
// the last_was_repeat_gap flag |
1052 |
if( mode2_data[mode2count] > 20000 && mode2_data[mode2count] < 70000 ) |
1053 |
{ |
1054 |
// printk(__FUNCTION__ " setting lwrg for val %d idx1 %d idx2 %d\n", |
1055 |
// mode2_data[mode2count], mode2count, dev->mode2_count+dev->mode2_idx-1 ); |
1056 |
dev->last_was_repeat_gap = 1; |
1057 |
} else { |
1058 |
dev->last_was_repeat_gap = 0; |
1059 |
} |
1060 |
|
1061 |
// this is a bit tricky; we need to change to a counter, but |
1062 |
// if we already had data in dev->mode2_data, then byte 0 |
1063 |
// actually was pre-existing data and shouldn't be counted |
1064 |
if( mode2count && !dev->mode2_count ) |
1065 |
{ |
1066 |
mode2count++; |
1067 |
// printk(__FUNCTION__ " mode2count++ to %d\n", mode2count); |
1068 |
} |
1069 |
|
1070 |
// never lie about how much output we have |
1071 |
dev->mode2_count += mode2count; |
1072 |
|
1073 |
if( last_pkt == 1 ) |
1074 |
{ |
1075 |
return bytecount; |
1076 |
} |
1077 |
else |
1078 |
{ |
1079 |
// note the partial pkt size, and make sure we only claim |
1080 |
// the bytes we processed |
1081 |
if( last_pkt == 2 ) |
1082 |
{ |
1083 |
dev->mode2_partial_pkt_size = split_pkt_size; |
1084 |
} |
1085 |
#if 1 |
1086 |
// XXX this i am not sure about; it seems like this should be required, but it |
1087 |
// isn't, and seems to cause problems |
1088 |
else |
1089 |
{ |
1090 |
dev->mode2_partial_pkt_size = bytes_left_in_packet; |
1091 |
} |
1092 |
#endif |
1093 |
return pos; |
1094 |
} |
1095 |
} |
1096 |
|
1097 |
static ssize_t mceusb_read( struct file* file, char* buffer, |
1098 |
size_t count, loff_t* ppos) |
1099 |
{ |
1100 |
char _data_buffer[128]; |
1101 |
struct usb_skel* dev; |
1102 |
int read_count; |
1103 |
int bytes_copied = 0; |
1104 |
|
1105 |
dev = (struct usb_skel*) file->private_data; |
1106 |
|
1107 |
if( (count % 4) != 0 ) |
1108 |
{ |
1109 |
return -EINVAL; |
1110 |
} |
1111 |
|
1112 |
down( &dev->sem ); |
1113 |
|
1114 |
/* verify that the device wasn't unplugged */ |
1115 |
if (dev->udev == NULL) { |
1116 |
up( &dev->sem ); |
1117 |
return -ENODEV; |
1118 |
} |
1119 |
|
1120 |
dbg(__FUNCTION__ " (1) calling msir_copy_mode2 with %d", count); |
1121 |
bytes_copied = 4 * msir_copy_mode2( dev, (int*)buffer, count >> 2 ); |
1122 |
if( bytes_copied == count ) |
1123 |
{ |
1124 |
up( &dev->sem ); |
1125 |
return count; |
1126 |
} |
1127 |
|
1128 |
/* we didn't get enough mode2 data. the process now is a bit complex |
1129 |
* 1. see if we have data read from the usb device that hasn't |
1130 |
* been converted to mode2; if so, convert that, and try to |
1131 |
* copy that out |
1132 |
* 2. otherwise, go ahead and read more, then convert that, then copy |
1133 |
*/ |
1134 |
|
1135 |
if( dev->usb_dcount ) |
1136 |
{ |
1137 |
read_count = msir_read_from_buffer( dev, _data_buffer, 128 ); |
1138 |
read_count = msir_generate_mode2 |
1139 |
( dev, (signed char*)_data_buffer, read_count ); |
1140 |
msir_mark_as_read( dev, read_count ); |
1141 |
bytes_copied += (4 * msir_copy_mode2 |
1142 |
( dev, (int*)(buffer + bytes_copied), |
1143 |
(count-bytes_copied) >> 2 )); |
1144 |
} |
1145 |
|
1146 |
if( bytes_copied == count ) |
1147 |
{ |
1148 |
up( &dev->sem ); |
1149 |
return count; |
1150 |
} |
1151 |
|
1152 |
/* read more data in a loop until we get enough */ |
1153 |
while( bytes_copied < count ) |
1154 |
{ |
1155 |
read_count = msir_fetch_more_data |
1156 |
( dev, (file->f_flags & O_NONBLOCK ? 1 : 0) ); |
1157 |
|
1158 |
if( read_count <= 0 ) |
1159 |
{ |
1160 |
up( &dev->sem ); |
1161 |
return (read_count ? read_count : -EWOULDBLOCK); |
1162 |
} |
1163 |
|
1164 |
read_count = msir_read_from_buffer( dev, _data_buffer, 128 ); |
1165 |
read_count = msir_generate_mode2 |
1166 |
( dev, (signed char*)_data_buffer, read_count ); |
1167 |
msir_mark_as_read( dev, read_count ); |
1168 |
|
1169 |
bytes_copied += (4 * msir_copy_mode2 |
1170 |
( dev, (int*)(buffer + bytes_copied), |
1171 |
(count-bytes_copied) >> 2 )); |
1172 |
} |
1173 |
|
1174 |
up( &dev->sem ); |
1175 |
return bytes_copied; |
1176 |
} |
1177 |
|
1178 |
/** |
1179 |
* mceusb_poll |
1180 |
*/ |
1181 |
static unsigned int mceusb_poll(struct file* file, poll_table* wait) |
1182 |
{ |
1183 |
struct usb_skel* dev; |
1184 |
int data; |
1185 |
dev = (struct usb_skel*)file->private_data; |
1186 |
|
1187 |
// So this is a crummy poll. Unfortunately all the lirc tools |
1188 |
// assume your hardware is interrupt driven. Instead, we have |
1189 |
// to actually read here to see whether or not there is data |
1190 |
// (unless we have a key saved up - unlikely ) |
1191 |
|
1192 |
// if( dev->usb_dcount || dev->mode2_count ) |
1193 |
if( msir_available_data( dev ) || msir_available_mode2( dev ) ) |
1194 |
{ |
1195 |
return POLLIN | POLLRDNORM; |
1196 |
} |
1197 |
else { |
1198 |
down( &dev->sem ); |
1199 |
data = msir_fetch_more_data( dev, 1 ); |
1200 |
up( &dev->sem ); |
1201 |
|
1202 |
if( data ) |
1203 |
return POLLIN | POLLRDNORM; |
1204 |
} |
1205 |
|
1206 |
return 0; |
1207 |
} |
1208 |
|
1209 |
/** |
1210 |
* mceusb_write |
1211 |
*/ |
1212 |
static ssize_t mceusb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos) |
1213 |
{ |
1214 |
struct usb_skel *dev; |
1215 |
ssize_t bytes_written = 0; |
1216 |
int retval = 0; |
1217 |
|
1218 |
dev = (struct usb_skel *)file->private_data; |
1219 |
|
1220 |
dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count); |
1221 |
|
1222 |
/* lock this object */ |
1223 |
down (&dev->sem); |
1224 |
|
1225 |
/* verify that the device wasn't unplugged */ |
1226 |
if (dev->udev == NULL) { |
1227 |
retval = -ENODEV; |
1228 |
goto exit; |
1229 |
} |
1230 |
|
1231 |
/* verify that we actually have some data to write */ |
1232 |
if (count == 0) { |
1233 |
dbg(__FUNCTION__ " - write request of 0 bytes"); |
1234 |
goto exit; |
1235 |
} |
1236 |
|
1237 |
/* see if we are already in the middle of a write */ |
1238 |
if (dev->write_urb->status == -EINPROGRESS) { |
1239 |
dbg (__FUNCTION__ " - already writing"); |
1240 |
goto exit; |
1241 |
} |
1242 |
|
1243 |
/* we can only write as much as 1 urb will hold */ |
1244 |
bytes_written = (count > dev->bulk_out_size) ? |
1245 |
dev->bulk_out_size : count; |
1246 |
|
1247 |
/* copy the data from userspace into our urb */ |
1248 |
if (copy_from_user(dev->write_urb->transfer_buffer, buffer, |
1249 |
bytes_written)) { |
1250 |
retval = -EFAULT; |
1251 |
goto exit; |
1252 |
} |
1253 |
|
1254 |
usb_mceusb_debug_data (__FUNCTION__, bytes_written, |
1255 |
dev->write_urb->transfer_buffer); |
1256 |
|
1257 |
/* set up our urb */ |
1258 |
FILL_BULK_URB(dev->write_urb, dev->udev, |
1259 |
usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr), |
1260 |
dev->write_urb->transfer_buffer, bytes_written, |
1261 |
mceusb_write_bulk_callback, dev); |
1262 |
|
1263 |
/* send the data out the bulk port */ |
1264 |
retval = usb_submit_urb(dev->write_urb); |
1265 |
if (retval) { |
1266 |
err(__FUNCTION__ " - failed submitting write urb, error %d", |
1267 |
retval); |
1268 |
} else { |
1269 |
retval = bytes_written; |
1270 |
} |
1271 |
|
1272 |
exit: |
1273 |
/* unlock the device */ |
1274 |
up (&dev->sem); |
1275 |
|
1276 |
return retval; |
1277 |
} |
1278 |
|
1279 |
/* |
1280 |
* mceusb_write_bulk_callback |
1281 |
*/ |
1282 |
|
1283 |
static void mceusb_write_bulk_callback (struct urb *urb) |
1284 |
{ |
1285 |
struct usb_skel *dev = (struct usb_skel *)urb->context; |
1286 |
|
1287 |
dbg(__FUNCTION__ " - minor %d", dev->minor); |
1288 |
|
1289 |
if ((urb->status != -ENOENT) && |
1290 |
(urb->status != -ECONNRESET)) { |
1291 |
dbg(__FUNCTION__ " - nonzero write bulk status received: %d", |
1292 |
urb->status); |
1293 |
return; |
1294 |
} |
1295 |
|
1296 |
return; |
1297 |
} |
1298 |
|
1299 |
/** |
1300 |
* mceusb_probe |
1301 |
* |
1302 |
* Called by the usb core when a new device is connected that it |
1303 |
* thinks this driver might be interested in. |
1304 |
*/ |
1305 |
static void * mceusb_probe(struct usb_device *udev, unsigned int ifnum, |
1306 |
const struct usb_device_id *id) |
1307 |
{ |
1308 |
struct usb_skel *dev = NULL; |
1309 |
struct usb_interface *interface; |
1310 |
struct usb_interface_descriptor *iface_desc; |
1311 |
struct usb_endpoint_descriptor *endpoint; |
1312 |
struct lirc_plugin* plugin; |
1313 |
struct lirc_buffer* rbuf; |
1314 |
|
1315 |
int minor; |
1316 |
int buffer_size; |
1317 |
int i; |
1318 |
char name[10]; |
1319 |
|
1320 |
|
1321 |
/* See if the device offered us matches what we can accept */ |
1322 |
if ((udev->descriptor.idVendor != USB_MCEUSB_VENDOR_ID) || |
1323 |
(udev->descriptor.idProduct != USB_MCEUSB_PRODUCT_ID)) { |
1324 |
return NULL; |
1325 |
} |
1326 |
|
1327 |
/* select a "subminor" number (part of a minor number) */ |
1328 |
down (&minor_table_mutex); |
1329 |
for (minor = 0; minor < MAX_DEVICES; ++minor) { |
1330 |
if (minor_table[minor] == NULL) |
1331 |
break; |
1332 |
} |
1333 |
if (minor >= MAX_DEVICES) { |
1334 |
info ("Too many devices plugged in, can not handle this device."); |
1335 |
goto exit; |
1336 |
} |
1337 |
|
1338 |
/* allocate memory for our device state and intialize it */ |
1339 |
dev = kmalloc (sizeof(struct usb_skel), GFP_KERNEL); |
1340 |
if (dev == NULL) { |
1341 |
err ("Out of memory"); |
1342 |
goto exit; |
1343 |
} |
1344 |
minor_table[minor] = dev; |
1345 |
|
1346 |
interface = &udev->actconfig->interface[ifnum]; |
1347 |
|
1348 |
init_MUTEX (&dev->sem); |
1349 |
dev->udev = udev; |
1350 |
dev->interface = interface; |
1351 |
dev->minor = minor; |
1352 |
|
1353 |
/* set up the endpoint information */ |
1354 |
/* check out the endpoints */ |
1355 |
iface_desc = &interface->altsetting[0]; |
1356 |
for (i = 0; i < iface_desc->bNumEndpoints; ++i) { |
1357 |
endpoint = &iface_desc->endpoint[i]; |
1358 |
|
1359 |
if ((endpoint->bEndpointAddress & 0x80) && |
1360 |
((endpoint->bmAttributes & 3) == 0x02)) { |
1361 |
/* we found a bulk in endpoint */ |
1362 |
buffer_size = endpoint->wMaxPacketSize; |
1363 |
dev->bulk_in_size = buffer_size; |
1364 |
dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; |
1365 |
dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL); |
1366 |
if (!dev->bulk_in_buffer) { |
1367 |
err("Couldn't allocate bulk_in_buffer"); |
1368 |
goto error; |
1369 |
} |
1370 |
} |
1371 |
|
1372 |
if (((endpoint->bEndpointAddress & 0x80) == 0x00) && |
1373 |
((endpoint->bmAttributes & 3) == 0x02)) { |
1374 |
/* we found a bulk out endpoint */ |
1375 |
dev->write_urb = usb_alloc_urb(0); |
1376 |
if (!dev->write_urb) { |
1377 |
err("No free urbs available"); |
1378 |
goto error; |
1379 |
} |
1380 |
buffer_size = endpoint->wMaxPacketSize; |
1381 |
dev->bulk_out_size = buffer_size; |
1382 |
dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; |
1383 |
dev->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL); |
1384 |
if (!dev->bulk_out_buffer) { |
1385 |
err("Couldn't allocate bulk_out_buffer"); |
1386 |
goto error; |
1387 |
} |
1388 |
FILL_BULK_URB(dev->write_urb, udev, |
1389 |
usb_sndbulkpipe |
1390 |
(udev, endpoint->bEndpointAddress), |
1391 |
dev->bulk_out_buffer, buffer_size, |
1392 |
mceusb_write_bulk_callback, dev); |
1393 |
} |
1394 |
} |
1395 |
|
1396 |
memset( dev->mode2_data, 0, sizeof( dev->mode2_data ) ); |
1397 |
dev->mode2_idx = 0; |
1398 |
dev->mode2_count = 0; |
1399 |
dev->mode2_partial_pkt_size = 0; |
1400 |
dev->mode2_once = 0; |
1401 |
dev->last_was_repeat_gap = 0; |
1402 |
|
1403 |
/* Set up our lirc plugin */ |
1404 |
if(!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) { |
1405 |
err("out of memory"); |
1406 |
goto error; |
1407 |
} |
1408 |
memset( plugin, 0, sizeof(struct lirc_plugin) ); |
1409 |
|
1410 |
if(!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) { |
1411 |
err("out of memory"); |
1412 |
kfree( plugin ); |
1413 |
goto error; |
1414 |
} |
1415 |
/* the lirc_atiusb module doesn't memset rbuf here ... ? */ |
1416 |
if( lirc_buffer_init( rbuf, sizeof(lirc_t), |
1417 |
sizeof(struct lirc_buffer))) { |
1418 |
err("out of memory"); |
1419 |
kfree( plugin ); |
1420 |
kfree( rbuf ); |
1421 |
goto error; |
1422 |
} |
1423 |
strcpy(plugin->name, "lirc_mce "); |
1424 |
plugin->minor = minor; |
1425 |
plugin->code_length = sizeof(lirc_t); |
1426 |
plugin->features = LIRC_CAN_REC_MODE2; // | LIRC_CAN_SEND_MODE2; |
1427 |
plugin->data = dev; |
1428 |
plugin->rbuf = rbuf; |
1429 |
plugin->ioctl = NULL; |
1430 |
plugin->set_use_inc = &set_use_inc; |
1431 |
plugin->set_use_dec = &set_use_dec; |
1432 |
plugin->fops = &mceusb_fops; |
1433 |
if( lirc_register_plugin( plugin ) < 0 ) |
1434 |
{ |
1435 |
kfree( plugin ); |
1436 |
lirc_buffer_free( rbuf ); |
1437 |
kfree( rbuf ); |
1438 |
goto error; |
1439 |
} |
1440 |
dev->plugin = plugin; |
1441 |
|
1442 |
mceusb_setup( udev ); |
1443 |
|
1444 |
/* let the user know what node this device is now attached to */ |
1445 |
info ("USB Microsoft IR Transceiver device now attached to msir%d", |
1446 |
dev->minor); |
1447 |
goto exit; |
1448 |
|
1449 |
error: |
1450 |
mceusb_delete (dev); |
1451 |
dev = NULL; |
1452 |
|
1453 |
exit: |
1454 |
up (&minor_table_mutex); |
1455 |
return dev; |
1456 |
} |
1457 |
|
1458 |
/** |
1459 |
* mceusb_disconnect |
1460 |
* |
1461 |
* Called by the usb core when the device is removed from the system. |
1462 |
*/ |
1463 |
static void mceusb_disconnect(struct usb_device *udev, void *ptr) |
1464 |
{ |
1465 |
int minor; |
1466 |
|
1467 |
down (&minor_table_mutex); |
1468 |
down (&dev->sem); |
1469 |
|
1470 |
minor = dev->minor; |
1471 |
|
1472 |
/* unhook lirc things */ |
1473 |
lirc_unregister_plugin( dev->minor ); |
1474 |
lirc_buffer_free( dev->plugin->rbuf ); |
1475 |
kfree( dev->plugin->rbuf ); |
1476 |
kfree( dev->plugin ); |
1477 |
|
1478 |
/* if the device is not opened, then we clean up right now */ |
1479 |
if (!dev->open_count) { |
1480 |
up (&dev->sem); |
1481 |
mceusb_delete (dev); |
1482 |
} else { |
1483 |
dev->udev = NULL; |
1484 |
up (&dev->sem); |
1485 |
} |
1486 |
|
1487 |
info("USB Skeleton #%d now disconnected", minor); |
1488 |
up (&minor_table_mutex); |
1489 |
return NULL; |
1490 |
} |
1491 |
|
1492 |
|
1493 |
|
1494 |
/** |
1495 |
* usb_mceusb_init |
1496 |
*/ |
1497 |
static int __init usb_mceusb_init(void) |
1498 |
{ |
1499 |
int result; |
1500 |
|
1501 |
/* register this driver with the USB subsystem */ |
1502 |
result = usb_register(&mceusb_driver); |
1503 |
if (result < 0) { |
1504 |
err("usb_register failed for the "__FILE__" driver. Error number %d", |
1505 |
result); |
1506 |
return -1; |
1507 |
} |
1508 |
|
1509 |
info(DRIVER_DESC " " DRIVER_VERSION); |
1510 |
return 0; |
1511 |
} |
1512 |
|
1513 |
|
1514 |
/** |
1515 |
* usb_mceusb_exit |
1516 |
*/ |
1517 |
static void __exit usb_mceusb_exit(void) |
1518 |
{ |
1519 |
/* deregister this driver with the USB subsystem */ |
1520 |
usb_deregister(&mceusb_driver); |
1521 |
} |
1522 |
|
1523 |
|
1524 |
module_init (usb_mceusb_init); |
1525 |
module_exit (usb_mceusb_exit); |
1526 |
|
1527 |
MODULE_AUTHOR(DRIVER_AUTHOR); |
1528 |
MODULE_DESCRIPTION(DRIVER_DESC); |
1529 |
MODULE_LICENSE("GPL"); |
1530 |
|