~ubuntu-branches/ubuntu/karmic/linux-fsl-imx51/karmic

« back to all changes in this revision

Viewing changes to ubuntu/lirc/lirc_mceusb2/lirc_mceusb2.c

  • Committer: Bazaar Package Importer
  • Author(s): Andy Whitcroft, Andy Whitcroft, Upstream Kernel Changes, Ubuntu: 2.6.31-14.46, Ubuntu: 2.6.31-13.45, Ubuntu: 2.6.31-13.44
  • Date: 2009-10-13 15:59:33 UTC
  • Revision ID: james.westby@ubuntu.com-20091013155933-hxzquigs9nqajd9x
Tags: 2.6.31-105.14
[ Andy Whitcroft ]

* rebase to Ubuntu-2.6.31-14.46
* [Config] update configs since rebase to Ubuntu-2.6.31-14.46
* lirc update dropped module lirc_mceusb2

[ Upstream Kernel Changes ]

* mxc spi: fix spi driver for 2.6.31
  - LP: #427289, #446140

[ Ubuntu: 2.6.31-14.46 ]

* reinstate armel.mk with no flavours
  - LP: #449637
* [Upstream] elevator: fix fastfail checks to allow merge of readahead
  requests
  - LP: #444915
* [Upstream] block: silently error unsupported empty barriers too
  - LP: #420423
* SAUCE: AppArmor: Fix mediation of "deleted" paths
  - LP: #415632
* [Config] CONFIG_X86_MCE
  https://lists.ubuntu.com/archives/kernel-team/2009-October/007584.html
* Revert "appletalk: Fix skb leak when ipddp interface is not loaded,
  CVE-2009-2903" - Use patch from 2.6.31.4 which is slightly different.
* x86: fix csum_ipv6_magic asm memory clobber
* tty: Avoid dropping ldisc_mutex over hangup tty re-initialization
* x86: Don't leak 64-bit kernel register values to 32-bit processes
* tracing: correct module boundaries for ftrace_release
* ftrace: check for failure for all conversions
* futex: fix requeue_pi key imbalance
* futex: Move exit_pi_state() call to release_mm()
* futex: Nullify robust lists after cleanup
* futex: Fix locking imbalance
* NOHZ: update idle state also when NOHZ is inactive
* ima: ecryptfs fix imbalance message
* libata: fix incorrect link online check during probe
* sound: via82xx: move DXS volume controls to PCM interface
* ASoC: WM8350 capture PGA mutes are inverted
* KVM: Prevent overflow in KVM_GET_SUPPORTED_CPUID
* KVM: VMX: flush TLB with INVEPT on cpu migration
* KVM: fix LAPIC timer period overflow
* KVM: SVM: Fix tsc offset adjustment when running nested
* KVM: SVM: Handle tsc in svm_get_msr/svm_set_msr correctly
* net: Fix wrong sizeof
* mm: add_to_swap_cache() must not sleep
* sis5513: fix PIO setup for ATAPI devices
* PIT fixes to unbreak suspend/resume (bug #14222)
* IMA: open new file for read
* ACPI: Clarify resource conflict message
* ACPI: fix Compaq Evo N800c (Pentium 4m) boot hang regression
* net: restore tx timestamping for accelerated vlans
* net: unix: fix sending fds in multiple buffers
* tun: Return -EINVAL if neither IFF_TUN nor IFF_TAP is set.
* tcp: fix CONFIG_TCP_MD5SIG + CONFIG_PREEMPT timer BUG()
* net: Fix sock_wfree() race
* smsc95xx: fix transmission where ZLP is expected
* sky2: Set SKY2_HW_RAM_BUFFER in sky2_init
* appletalk: Fix skb leak when ipddp interface is not loaded
* ax25: Fix possible oops in ax25_make_new
* ax25: Fix SIOCAX25GETINFO ioctl
* sit: fix off-by-one in ipip6_tunnel_get_prl
* Linux 2.6.31.4
* drm/i915: Fix FDI M/N setting according with correct color depth
  - LP: #416792

[ Ubuntu: 2.6.31-13.45 ]

* [Config] Add sd_mod to scsi-modules udeb for powerpc
* SAUCE: Update to LIRC 0.8.6
  - LP: #432678
* [Config] CONFIG_X86_PAT=y
  https://lists.ubuntu.com/archives/kernel-team/2009-October/007477.html
  https://lists.ubuntu.com/archives/kernel-team/2009-October/007534.html
* [Config] Add armel arch to linux-libc-dev arches.
  - LP: #449637
* e1000e: swap max hw supported frame size between 82574 and 82583
* drm/i915: Initialize HDMI outputs as HDMI connectors, not DVI.
  - LP: #392017
* ALSA: hda - Add quirks for some HP laptops
  - LP: #449742
* ALSA: hda - Add support for HP dv6
  - LP: #449742

[ Ubuntu: 2.6.31-13.44 ]

* sgi-gru: Fix kernel stack buffer overrun, CVE-2009-2584
* appletalk: Fix skb leak when ipddp interface is not loaded,
  CVE-2009-2903
  Note - This patch causes an ABI change in the symbol aarp_send_ddp
  which I'm ignoring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * LIRC driver for Philips eHome USB Infrared Transceiver
3
 
 * and the Microsoft MCE 2005 Remote Control
4
 
 *
5
 
 * (C) by Martin A. Blatter <martin_a_blatter@yahoo.com>
6
 
 *
7
 
 * Transmitter support and reception code cleanup.
8
 
 * (C) by Daniel Melander <lirc@rajidae.se>
9
 
 *
10
 
 * Derived from ATI USB driver by Paul Miller and the original
11
 
 * MCE USB driver by Dan Corti
12
 
 *
13
 
 * This driver will only work reliably with kernel version 2.6.10
14
 
 * or higher, probably because of differences in USB device enumeration
15
 
 * in the kernel code. Device initialization fails most of the time
16
 
 * with earlier kernel versions.
17
 
 *
18
 
 **********************************************************************
19
 
 *
20
 
 * This program is free software; you can redistribute it and/or modify
21
 
 * it under the terms of the GNU General Public License as published by
22
 
 * the Free Software Foundation; either version 2 of the License, or
23
 
 * (at your option) any later version.
24
 
 *
25
 
 * This program is distributed in the hope that it will be useful,
26
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 
 * GNU General Public License for more details.
29
 
 *
30
 
 * You should have received a copy of the GNU General Public License
31
 
 * along with this program; if not, write to the Free Software
32
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33
 
 *
34
 
 */
35
 
 
36
 
#include <linux/version.h>
37
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
38
 
#error "*******************************************************"
39
 
#error "Sorry, this driver needs kernel version 2.6.5 or higher"
40
 
#error "*******************************************************"
41
 
#endif
42
 
#include <linux/autoconf.h>
43
 
#include <linux/kernel.h>
44
 
#include <linux/errno.h>
45
 
#include <linux/init.h>
46
 
#include <linux/slab.h>
47
 
#include <linux/module.h>
48
 
#include <linux/kmod.h>
49
 
#include <linux/smp_lock.h>
50
 
#include <linux/completion.h>
51
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
52
 
#include <asm/uaccess.h>
53
 
#else
54
 
#include <linux/uaccess.h>
55
 
#endif
56
 
#include <linux/usb.h>
57
 
#include <linux/wait.h>
58
 
#include <linux/time.h>
59
 
 
60
 
#include "../lirc.h"
61
 
#include "../kcompat.h"
62
 
#include "../lirc_dev/lirc_dev.h"
63
 
 
64
 
#define DRIVER_VERSION  "$Revision: 1.81 $"
65
 
#define DRIVER_AUTHOR   "Daniel Melander <lirc@rajidae.se>, " \
66
 
                        "Martin Blatter <martin_a_blatter@yahoo.com>"
67
 
#define DRIVER_DESC     "Philips eHome USB IR Transceiver and Microsoft " \
68
 
                        "MCE 2005 Remote Control driver for LIRC"
69
 
#define DRIVER_NAME     "lirc_mceusb2"
70
 
 
71
 
#define USB_BUFLEN      32      /* USB reception buffer length */
72
 
#define LIRCBUF_SIZE    256     /* LIRC work buffer length */
73
 
 
74
 
/* MCE constants */
75
 
#define MCE_CMDBUF_SIZE 384 /* MCE Command buffer length */
76
 
#define MCE_TIME_UNIT   50 /* Approx 50us resolution */
77
 
#define MCE_CODE_LENGTH 5 /* Normal length of packet (with header) */
78
 
#define MCE_PACKET_SIZE 4 /* Normal length of packet (without header) */
79
 
#define MCE_PACKET_HEADER 0x84 /* Actual header format is 0x80 + num_bytes */
80
 
#define MCE_CONTROL_HEADER 0x9F /* MCE status header */
81
 
#define MCE_TX_HEADER_LENGTH 3 /* # of bytes in the initializing tx header */
82
 
#define MCE_MAX_CHANNELS 2 /* Two transmitters, hardware dependent? */
83
 
#define MCE_DEFAULT_TX_MASK 0x03 /* Val opts: TX1=0x01, TX2=0x02, ALL=0x03 */
84
 
#define MCE_PULSE_BIT   0x80 /* Pulse bit, MSB set == PULSE else SPACE */
85
 
#define MCE_PULSE_MASK  0x7F /* Pulse mask */
86
 
#define MCE_MAX_PULSE_LENGTH 0x7F /* Longest transmittable pulse symbol */
87
 
#define MCE_PACKET_LENGTH_MASK  0x7F /* Pulse mask */
88
 
 
89
 
 
90
 
/* module parameters */
91
 
#ifdef CONFIG_USB_DEBUG
92
 
static int debug = 1;
93
 
#else
94
 
static int debug;
95
 
#endif
96
 
#define dprintk(fmt, args...)                                   \
97
 
        do {                                                    \
98
 
                if (debug)                                      \
99
 
                        printk(KERN_DEBUG fmt, ## args);        \
100
 
        } while (0)
101
 
 
102
 
/* general constants */
103
 
#define SEND_FLAG_IN_PROGRESS   1
104
 
#define SEND_FLAG_COMPLETE      2
105
 
#define RECV_FLAG_IN_PROGRESS   3
106
 
#define RECV_FLAG_COMPLETE      4
107
 
 
108
 
#define PHILUSB_INBOUND         1
109
 
#define PHILUSB_OUTBOUND        2
110
 
 
111
 
#define VENDOR_PHILIPS          0x0471
112
 
#define VENDOR_SMK              0x0609
113
 
#define VENDOR_TATUNG           0x1460
114
 
#define VENDOR_GATEWAY          0x107b
115
 
#define VENDOR_SHUTTLE          0x1308
116
 
#define VENDOR_SHUTTLE2         0x051c
117
 
#define VENDOR_MITSUMI          0x03ee
118
 
#define VENDOR_TOPSEED          0x1784
119
 
#define VENDOR_RICAVISION       0x179d
120
 
#define VENDOR_ITRON            0x195d
121
 
#define VENDOR_FIC              0x1509
122
 
#define VENDOR_LG               0x043e
123
 
#define VENDOR_MICROSOFT        0x045e
124
 
#define VENDOR_FORMOSA          0x147a
125
 
#define VENDOR_FINTEK           0x1934
126
 
#define VENDOR_PINNACLE         0x2304
127
 
#define VENDOR_ECS              0x1019
128
 
#define VENDOR_WISTRON          0x0fb8
129
 
#define VENDOR_COMPRO           0x185b
130
 
#define VENDOR_NORTHSTAR        0x04eb
131
 
 
132
 
static struct usb_device_id mceusb_dev_table[] = {
133
 
        /* Philips Infrared Transceiver - Sahara branded */
134
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x0608) },
135
 
        /* Philips Infrared Transceiver - HP branded */
136
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x060c) },
137
 
        /* Philips SRM5100 */
138
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x060d) },
139
 
        /* Philips Infrared Transceiver - Omaura */
140
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x060f) },
141
 
        /* Philips Infrared Transceiver - Spinel plus */
142
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x0613) },
143
 
        /* Philips eHome Infrared Transceiver */
144
 
        { USB_DEVICE(VENDOR_PHILIPS, 0x0815) },
145
 
        /* SMK/Toshiba G83C0004D410 */
146
 
        { USB_DEVICE(VENDOR_SMK, 0x031d) },
147
 
        /* SMK eHome Infrared Transceiver (Sony VAIO) */
148
 
        { USB_DEVICE(VENDOR_SMK, 0x0322) },
149
 
        /* bundled with Hauppauge PVR-150 */
150
 
        { USB_DEVICE(VENDOR_SMK, 0x0334) },
151
 
        /* Tatung eHome Infrared Transceiver */
152
 
        { USB_DEVICE(VENDOR_TATUNG, 0x9150) },
153
 
        /* Shuttle eHome Infrared Transceiver */
154
 
        { USB_DEVICE(VENDOR_SHUTTLE, 0xc001) },
155
 
        /* Shuttle eHome Infrared Transceiver */
156
 
        { USB_DEVICE(VENDOR_SHUTTLE2, 0xc001) },
157
 
        /* Gateway eHome Infrared Transceiver */
158
 
        { USB_DEVICE(VENDOR_GATEWAY, 0x3009) },
159
 
        /* Mitsumi */
160
 
        { USB_DEVICE(VENDOR_MITSUMI, 0x2501) },
161
 
        /* Topseed eHome Infrared Transceiver */
162
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
163
 
        /* Topseed HP eHome Infrared Transceiver */
164
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0006) },
165
 
        /* Topseed eHome Infrared Transceiver */
166
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
167
 
        /* Topseed eHome Infrared Transceiver */
168
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
169
 
        /* Ricavision internal Infrared Transceiver */
170
 
        { USB_DEVICE(VENDOR_RICAVISION, 0x0010) },
171
 
        /* Itron ione Libra Q-11 */
172
 
        { USB_DEVICE(VENDOR_ITRON, 0x7002) },
173
 
        /* FIC eHome Infrared Transceiver */
174
 
        { USB_DEVICE(VENDOR_FIC, 0x9242) },
175
 
        /* LG eHome Infrared Transceiver */
176
 
        { USB_DEVICE(VENDOR_LG, 0x9803) },
177
 
        /* Microsoft MCE Infrared Transceiver */
178
 
        { USB_DEVICE(VENDOR_MICROSOFT, 0x00a0) },
179
 
        /* Formosa eHome Infrared Transceiver */
180
 
        { USB_DEVICE(VENDOR_FORMOSA, 0xe015) },
181
 
        /* Formosa21 / eHome Infrared Receiver */
182
 
        { USB_DEVICE(VENDOR_FORMOSA, 0xe016) },
183
 
        /* Formosa aim / Trust MCE Infrared Receiver */
184
 
        { USB_DEVICE(VENDOR_FORMOSA, 0xe017) },
185
 
        /* Formosa Industrial Computing / Beanbag Emulation Device */
186
 
        { USB_DEVICE(VENDOR_FORMOSA, 0xe018) },
187
 
        /* Fintek eHome Infrared Transceiver */
188
 
        { USB_DEVICE(VENDOR_FINTEK, 0x0602) },
189
 
        /* Fintek eHome Infrared Transceiver (in the AOpen MP45) */
190
 
        { USB_DEVICE(VENDOR_FINTEK, 0x0702) },
191
 
        /* Pinnacle Remote Kit */
192
 
        { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
193
 
        /* Elitegroup Computer Systems IR */
194
 
        { USB_DEVICE(VENDOR_ECS, 0x0f38) },
195
 
        /* Wistron Corp. eHome Infrared Receiver */
196
 
        { USB_DEVICE(VENDOR_WISTRON, 0x0002) },
197
 
        /* Compro K100 */
198
 
        { USB_DEVICE(VENDOR_COMPRO, 0x3020) },
199
 
        /* Northstar Systems eHome Infrared Transceiver */
200
 
        { USB_DEVICE(VENDOR_NORTHSTAR, 0xe004) },
201
 
        /* Terminating entry */
202
 
        { }
203
 
};
204
 
 
205
 
static struct usb_device_id pinnacle_list[] = {
206
 
        { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
207
 
        {}
208
 
};
209
 
 
210
 
static struct usb_device_id transmitter_mask_list[] = {
211
 
        { USB_DEVICE(VENDOR_SMK, 0x031d) },
212
 
        { USB_DEVICE(VENDOR_SMK, 0x0322) },
213
 
        { USB_DEVICE(VENDOR_SMK, 0x0334) },
214
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0001) },
215
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0007) },
216
 
        { USB_DEVICE(VENDOR_TOPSEED, 0x0008) },
217
 
        { USB_DEVICE(VENDOR_PINNACLE, 0x0225) },
218
 
        {}
219
 
};
220
 
 
221
 
/* data structure for each usb transceiver */
222
 
struct mceusb2_dev {
223
 
 
224
 
        /* usb */
225
 
        struct usb_device *usbdev;
226
 
        struct urb *urb_in;
227
 
        int devnum;
228
 
        struct usb_endpoint_descriptor *usb_ep_in;
229
 
        struct usb_endpoint_descriptor *usb_ep_out;
230
 
 
231
 
        /* buffers and dma */
232
 
        unsigned char *buf_in;
233
 
        unsigned int len_in;
234
 
        dma_addr_t dma_in;
235
 
        dma_addr_t dma_out;
236
 
 
237
 
        /* lirc */
238
 
        struct lirc_driver *d;
239
 
        lirc_t lircdata;
240
 
        unsigned char is_pulse;
241
 
        struct {
242
 
                u32 connected:1;
243
 
                u32 pinnacle:1;
244
 
                u32 transmitter_mask_inverted:1;
245
 
                u32 reserved:29;
246
 
        } flags;
247
 
 
248
 
        unsigned char transmitter_mask;
249
 
        unsigned int carrier_freq;
250
 
 
251
 
        /* handle sending (init strings) */
252
 
        int send_flags;
253
 
        wait_queue_head_t wait_out;
254
 
 
255
 
        struct mutex lock;
256
 
};
257
 
 
258
 
/* init strings */
259
 
static char init1[] = {0x00, 0xff, 0xaa, 0xff, 0x0b};
260
 
static char init2[] = {0xff, 0x18};
261
 
 
262
 
static char pin_init1[] = { 0x9f, 0x07};
263
 
static char pin_init2[] = { 0x9f, 0x13};
264
 
static char pin_init3[] = { 0x9f, 0x0d};
265
 
 
266
 
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11)
267
 
static unsigned long usecs_to_jiffies(const unsigned int u)
268
 
{
269
 
        if (u > jiffies_to_usecs(MAX_JIFFY_OFFSET))
270
 
                return MAX_JIFFY_OFFSET;
271
 
#if HZ <= USEC_PER_SEC && !(USEC_PER_SEC % HZ)
272
 
        return (u + (USEC_PER_SEC / HZ) - 1) / (USEC_PER_SEC / HZ);
273
 
#elif HZ > USEC_PER_SEC && !(HZ % USEC_PER_SEC)
274
 
        return u * (HZ / USEC_PER_SEC);
275
 
#else
276
 
        return (u * HZ + USEC_PER_SEC - 1) / USEC_PER_SEC;
277
 
#endif
278
 
}
279
 
#endif
280
 
static void mceusb_dev_printdata(struct mceusb2_dev *ir, char *buf, int len)
281
 
{
282
 
        char codes[USB_BUFLEN*3 + 1];
283
 
        int i;
284
 
 
285
 
        if (len <= 0)
286
 
                return;
287
 
 
288
 
        for (i = 0; i < len && i < USB_BUFLEN; i++)
289
 
                snprintf(codes+i*3, 4, "%02x ", buf[i] & 0xFF);
290
 
 
291
 
        printk(KERN_INFO "" DRIVER_NAME "[%d]: data received %s (length=%d)\n",
292
 
                ir->devnum, codes, len);
293
 
}
294
 
 
295
 
static void usb_async_callback(struct urb *urb, struct pt_regs *regs)
296
 
{
297
 
        struct mceusb2_dev *ir;
298
 
        int len;
299
 
 
300
 
        if (!urb)
301
 
                return;
302
 
 
303
 
        ir = urb->context;
304
 
        if (ir) {
305
 
                len = urb->actual_length;
306
 
 
307
 
                dprintk(DRIVER_NAME
308
 
                        "[%d]: callback called (status=%d len=%d)\n",
309
 
                        ir->devnum, urb->status, len);
310
 
 
311
 
                if (debug)
312
 
                        mceusb_dev_printdata(ir, urb->transfer_buffer, len);
313
 
        }
314
 
 
315
 
}
316
 
 
317
 
 
318
 
/* request incoming or send outgoing usb packet - used to initialize remote */
319
 
static void request_packet_async(struct mceusb2_dev *ir,
320
 
                                 struct usb_endpoint_descriptor *ep,
321
 
                                 unsigned char *data, int size, int urb_type)
322
 
{
323
 
        int res;
324
 
        struct urb *async_urb;
325
 
        unsigned char *async_buf;
326
 
 
327
 
        if (urb_type) {
328
 
                async_urb = usb_alloc_urb(0, GFP_KERNEL);
329
 
                if (likely(async_urb)) {
330
 
                        /* alloc buffer */
331
 
                        async_buf = kmalloc(size, GFP_KERNEL);
332
 
                        if (async_buf) {
333
 
                                if (urb_type == PHILUSB_OUTBOUND) {
334
 
                                        /* outbound data */
335
 
                                        usb_fill_int_urb(async_urb, ir->usbdev,
336
 
                                                usb_sndintpipe(ir->usbdev,
337
 
                                                        ep->bEndpointAddress),
338
 
                                        async_buf,
339
 
                                        size,
340
 
                                        (usb_complete_t) usb_async_callback,
341
 
                                        ir, ep->bInterval);
342
 
 
343
 
                                        memcpy(async_buf, data, size);
344
 
                                } else {
345
 
                                        /* inbound data */
346
 
                                        usb_fill_int_urb(async_urb, ir->usbdev,
347
 
                                                usb_rcvintpipe(ir->usbdev,
348
 
                                                        ep->bEndpointAddress),
349
 
                                        async_buf, size,
350
 
                                        (usb_complete_t) usb_async_callback,
351
 
                                        ir, ep->bInterval);
352
 
                                }
353
 
                                async_urb->transfer_flags = URB_ASYNC_UNLINK;
354
 
                        } else {
355
 
                                usb_free_urb(async_urb);
356
 
                                return;
357
 
                        }
358
 
                }
359
 
        } else {
360
 
                /* standard request */
361
 
                async_urb = ir->urb_in;
362
 
                ir->send_flags = RECV_FLAG_IN_PROGRESS;
363
 
        }
364
 
        dprintk(DRIVER_NAME "[%d]: receive request called (size=%#x)\n",
365
 
                ir->devnum, size);
366
 
 
367
 
        async_urb->transfer_buffer_length = size;
368
 
        async_urb->dev = ir->usbdev;
369
 
 
370
 
        res = usb_submit_urb(async_urb, GFP_ATOMIC);
371
 
        if (res) {
372
 
                dprintk(DRIVER_NAME "[%d]: receive request FAILED! (res=%d)\n",
373
 
                        ir->devnum, res);
374
 
                return;
375
 
        }
376
 
        dprintk(DRIVER_NAME "[%d]: receive request complete (res=%d)\n",
377
 
                ir->devnum, res);
378
 
}
379
 
 
380
 
static int unregister_from_lirc(struct mceusb2_dev *ir)
381
 
{
382
 
        struct lirc_driver *d = ir->d;
383
 
        int devnum;
384
 
        int rtn;
385
 
 
386
 
        devnum = ir->devnum;
387
 
        dprintk(DRIVER_NAME "[%d]: unregister from lirc called\n", devnum);
388
 
 
389
 
        rtn = lirc_unregister_driver(d->minor);
390
 
        if (rtn > 0) {
391
 
                printk(DRIVER_NAME "[%d]: error in lirc_unregister minor: %d\n"
392
 
                        "Trying again...\n", devnum, d->minor);
393
 
                if (rtn == -EBUSY) {
394
 
                        printk(DRIVER_NAME
395
 
                                "[%d]: device is opened, will unregister"
396
 
                                " on close\n", devnum);
397
 
                        return -EAGAIN;
398
 
                }
399
 
                set_current_state(TASK_INTERRUPTIBLE);
400
 
                schedule_timeout(HZ);
401
 
 
402
 
                rtn = lirc_unregister_driver(d->minor);
403
 
                if (rtn > 0)
404
 
                        printk(DRIVER_NAME "[%d]: lirc_unregister failed\n",
405
 
                        devnum);
406
 
        }
407
 
 
408
 
        if (rtn) {
409
 
                printk(DRIVER_NAME "[%d]: didn't free resources\n", devnum);
410
 
                return -EAGAIN;
411
 
        }
412
 
 
413
 
        printk(DRIVER_NAME "[%d]: usb remote disconnected\n", devnum);
414
 
 
415
 
        lirc_buffer_free(d->rbuf);
416
 
        kfree(d->rbuf);
417
 
        kfree(d);
418
 
        kfree(ir);
419
 
        return 0;
420
 
}
421
 
 
422
 
static int set_use_inc(void *data)
423
 
{
424
 
        struct mceusb2_dev *ir = data;
425
 
 
426
 
        if (!ir) {
427
 
                printk(DRIVER_NAME "[?]: set_use_inc called with no context\n");
428
 
                return -EIO;
429
 
        }
430
 
        dprintk(DRIVER_NAME "[%d]: set use inc\n", ir->devnum);
431
 
 
432
 
        MOD_INC_USE_COUNT;
433
 
        if (!ir->flags.connected) {
434
 
                if (!ir->usbdev)
435
 
                        return -ENOENT;
436
 
                ir->flags.connected = 1;
437
 
        }
438
 
 
439
 
        return 0;
440
 
}
441
 
 
442
 
static void set_use_dec(void *data)
443
 
{
444
 
        struct mceusb2_dev *ir = data;
445
 
 
446
 
        if (!ir) {
447
 
                printk(DRIVER_NAME "[?]: set_use_dec called with no context\n");
448
 
                return;
449
 
        }
450
 
        dprintk(DRIVER_NAME "[%d]: set use dec\n", ir->devnum);
451
 
 
452
 
        if (ir->flags.connected) {
453
 
                mutex_lock(&ir->lock);
454
 
                ir->flags.connected = 0;
455
 
                mutex_unlock(&ir->lock);
456
 
        }
457
 
        MOD_DEC_USE_COUNT;
458
 
}
459
 
 
460
 
static void send_packet_to_lirc(struct mceusb2_dev *ir)
461
 
{
462
 
        if (ir->lircdata != 0) {
463
 
                lirc_buffer_write(ir->d->rbuf,
464
 
                                  (unsigned char *) &ir->lircdata);
465
 
                wake_up(&ir->d->rbuf->wait_poll);
466
 
                ir->lircdata = 0;
467
 
        }
468
 
}
469
 
 
470
 
static void mceusb_dev_recv(struct urb *urb, struct pt_regs *regs)
471
 
{
472
 
        struct mceusb2_dev *ir;
473
 
        int buf_len, packet_len;
474
 
        int i, j;
475
 
 
476
 
        if (!urb)
477
 
                return;
478
 
 
479
 
        ir = urb->context;
480
 
        if (!ir) {
481
 
                urb->transfer_flags |= URB_ASYNC_UNLINK;
482
 
                usb_unlink_urb(urb);
483
 
                return;
484
 
        }
485
 
 
486
 
        buf_len = urb->actual_length;
487
 
        packet_len = 0;
488
 
 
489
 
        if (debug)
490
 
                mceusb_dev_printdata(ir, urb->transfer_buffer, buf_len);
491
 
 
492
 
        if (ir->send_flags == RECV_FLAG_IN_PROGRESS) {
493
 
                ir->send_flags = SEND_FLAG_COMPLETE;
494
 
                dprintk(DRIVER_NAME "[%d]: setup answer received %d bytes\n",
495
 
                        ir->devnum, buf_len);
496
 
        }
497
 
 
498
 
        switch (urb->status) {
499
 
        /* success */
500
 
        case 0:
501
 
                for (i = 0; i < buf_len; i++) {
502
 
                        /* decode mce packets of the form (84),AA,BB,CC,DD */
503
 
                        if (ir->buf_in[i] >= 0x80 && ir->buf_in[i] <= 0x9e) {
504
 
                                /* data headers */
505
 
                                /* decode packet data */
506
 
                                packet_len =
507
 
                                        ir->buf_in[i] & MCE_PACKET_LENGTH_MASK;
508
 
                                for (j = 1;
509
 
                                     j <= packet_len && (i+j < buf_len);
510
 
                                     j++) {
511
 
                                        /* rising/falling flank */
512
 
                                        if (ir->is_pulse !=
513
 
                                            (ir->buf_in[i + j] &
514
 
                                             MCE_PULSE_BIT)) {
515
 
                                                send_packet_to_lirc(ir);
516
 
                                                ir->is_pulse =
517
 
                                                        ir->buf_in[i + j] &
518
 
                                                                MCE_PULSE_BIT;
519
 
                                        }
520
 
 
521
 
                                        /* accumulate mce pulse/space values */
522
 
                                        ir->lircdata +=
523
 
                                                (ir->buf_in[i + j] &
524
 
                                                 MCE_PULSE_MASK)*MCE_TIME_UNIT;
525
 
                                        ir->lircdata |=
526
 
                                                (ir->is_pulse ? PULSE_BIT : 0);
527
 
                                }
528
 
 
529
 
                                i += packet_len;
530
 
                        } else if (ir->buf_in[i] == MCE_CONTROL_HEADER) {
531
 
                                /* status header (0x9F) */
532
 
                                /*
533
 
                                 * A transmission containing one or
534
 
                                 * more consecutive ir commands always
535
 
                                 * ends with a GAP of 100ms followed by the
536
 
                                 * sequence 0x9F 0x01 0x01 0x9F 0x15
537
 
                                 * 0x00 0x00 0x80
538
 
                                 */
539
 
 
540
 
                /*
541
 
                Uncomment this if the last 100ms
542
 
                "infinity"-space should be transmitted
543
 
                to lirc directly instead of at the beginning
544
 
                of the next transmission. Changes pulse/space order.
545
 
 
546
 
                                if (++i < buf_len && ir->buf_in[i]==0x01)
547
 
                                        send_packet_to_lirc(ir);
548
 
 
549
 
                */
550
 
 
551
 
                                /* end decode loop */
552
 
                                i = buf_len;
553
 
                        }
554
 
                }
555
 
 
556
 
                break;
557
 
 
558
 
        case -ECONNRESET:
559
 
        case -ENOENT:
560
 
        case -ESHUTDOWN:
561
 
                urb->transfer_flags |= URB_ASYNC_UNLINK;
562
 
                usb_unlink_urb(urb);
563
 
                return;
564
 
 
565
 
        case -EPIPE:
566
 
        default:
567
 
                break;
568
 
        }
569
 
 
570
 
        usb_submit_urb(urb, GFP_ATOMIC);
571
 
}
572
 
 
573
 
 
574
 
static ssize_t lirc_write(struct file *file, const char *buf,
575
 
                          size_t n, loff_t *ppos)
576
 
{
577
 
        int i, count = 0, cmdcount = 0;
578
 
        struct mceusb2_dev *ir = NULL;
579
 
        lirc_t wbuf[LIRCBUF_SIZE]; /* Workbuffer with values from lirc */
580
 
        unsigned char cmdbuf[MCE_CMDBUF_SIZE]; /* MCE command buffer */
581
 
        unsigned long signal_duration = 0; /* Singnal length in us */
582
 
        struct timeval start_time, end_time;
583
 
 
584
 
        do_gettimeofday(&start_time);
585
 
 
586
 
        /* Retrieve lirc_driver data for the device */
587
 
        ir = lirc_get_pdata(file);
588
 
        if (!ir || !ir->usb_ep_out)
589
 
                return -EFAULT;
590
 
 
591
 
        if (n % sizeof(lirc_t))
592
 
                return -EINVAL;
593
 
        count = n / sizeof(lirc_t);
594
 
 
595
 
        /* Check if command is within limits */
596
 
        if (count > LIRCBUF_SIZE || count%2 == 0)
597
 
                return -EINVAL;
598
 
        if (copy_from_user(wbuf, buf, n))
599
 
                return -EFAULT;
600
 
 
601
 
        /* MCE tx init header */
602
 
        cmdbuf[cmdcount++] = MCE_CONTROL_HEADER;
603
 
        cmdbuf[cmdcount++] = 0x08;
604
 
        cmdbuf[cmdcount++] = ir->transmitter_mask;
605
 
 
606
 
        /* Generate mce packet data */
607
 
        for (i = 0; (i < count) && (cmdcount < MCE_CMDBUF_SIZE); i++) {
608
 
                signal_duration += wbuf[i];
609
 
                wbuf[i] = wbuf[i] / MCE_TIME_UNIT;
610
 
 
611
 
                do { /* loop to support long pulses/spaces > 127*50us=6.35ms */
612
 
 
613
 
                        /* Insert mce packet header every 4th entry */
614
 
                        if ((cmdcount < MCE_CMDBUF_SIZE) &&
615
 
                            (cmdcount - MCE_TX_HEADER_LENGTH) %
616
 
                             MCE_CODE_LENGTH == 0)
617
 
                                cmdbuf[cmdcount++] = MCE_PACKET_HEADER;
618
 
 
619
 
                        /* Insert mce packet data */
620
 
                        if (cmdcount < MCE_CMDBUF_SIZE)
621
 
                                cmdbuf[cmdcount++] =
622
 
                                        (wbuf[i] < MCE_PULSE_BIT ?
623
 
                                         wbuf[i] : MCE_MAX_PULSE_LENGTH) |
624
 
                                         (i & 1 ? 0x00 : MCE_PULSE_BIT);
625
 
                        else
626
 
                                return -EINVAL;
627
 
                } while ((wbuf[i] > MCE_MAX_PULSE_LENGTH) &&
628
 
                         (wbuf[i] -= MCE_MAX_PULSE_LENGTH));
629
 
        }
630
 
 
631
 
        /* Fix packet length in last header */
632
 
        cmdbuf[cmdcount - (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH] =
633
 
                0x80 + (cmdcount - MCE_TX_HEADER_LENGTH) % MCE_CODE_LENGTH - 1;
634
 
 
635
 
        /* Check if we have room for the empty packet at the end */
636
 
        if (cmdcount >= MCE_CMDBUF_SIZE)
637
 
                return -EINVAL;
638
 
 
639
 
        /* All mce commands end with an empty packet (0x80) */
640
 
        cmdbuf[cmdcount++] = 0x80;
641
 
 
642
 
        /* Transmit the command to the mce device */
643
 
        request_packet_async(ir, ir->usb_ep_out, cmdbuf,
644
 
                             cmdcount, PHILUSB_OUTBOUND);
645
 
 
646
 
        /*
647
 
         * The lircd gap calculation expects the write function to
648
 
         * wait the time it takes for the ircommand to be sent before
649
 
         * it returns.
650
 
         */
651
 
        do_gettimeofday(&end_time);
652
 
        signal_duration -= (end_time.tv_usec - start_time.tv_usec) +
653
 
                           (end_time.tv_sec - start_time.tv_sec) * 1000000;
654
 
 
655
 
        /* delay with the closest number of ticks */
656
 
        set_current_state(TASK_INTERRUPTIBLE);
657
 
        schedule_timeout(usecs_to_jiffies(signal_duration));
658
 
 
659
 
        return n;
660
 
}
661
 
 
662
 
static void set_transmitter_mask(struct mceusb2_dev *ir, unsigned int mask)
663
 
{
664
 
        if (ir->flags.transmitter_mask_inverted)
665
 
                /*
666
 
                 * The mask begins at 0x02 and has an inverted
667
 
                 * numbering scheme
668
 
                 */
669
 
                ir->transmitter_mask =
670
 
                        (mask != 0x03 ? mask ^ 0x03 : mask) << 1;
671
 
        else
672
 
                ir->transmitter_mask = mask;
673
 
}
674
 
 
675
 
 
676
 
/* Sets the send carrier frequency */
677
 
static int set_send_carrier(struct mceusb2_dev *ir, int carrier)
678
 
{
679
 
        int clk = 10000000;
680
 
        int prescaler = 0, divisor = 0;
681
 
        unsigned char cmdbuf[] = { 0x9F, 0x06, 0x01, 0x80 };
682
 
 
683
 
        /* Carrier is changed */
684
 
        if (ir->carrier_freq != carrier) {
685
 
 
686
 
                if (carrier <= 0) {
687
 
                        ir->carrier_freq = carrier;
688
 
                        dprintk(DRIVER_NAME "[%d]: SET_CARRIER disabling "
689
 
                                "carrier modulation\n", ir->devnum);
690
 
                        request_packet_async(ir, ir->usb_ep_out,
691
 
                                             cmdbuf, sizeof(cmdbuf),
692
 
                                             PHILUSB_OUTBOUND);
693
 
                        return carrier;
694
 
                }
695
 
 
696
 
                for (prescaler = 0; prescaler < 4; ++prescaler) {
697
 
                        divisor = (clk >> (2 * prescaler)) / carrier;
698
 
                        if (divisor <= 0xFF) {
699
 
                                ir->carrier_freq = carrier;
700
 
                                cmdbuf[2] = prescaler;
701
 
                                cmdbuf[3] = divisor;
702
 
                                dprintk(DRIVER_NAME "[%d]: SET_CARRIER "
703
 
                                        "requesting %d Hz\n",
704
 
                                        ir->devnum, carrier);
705
 
 
706
 
                                /* Transmit new carrier to mce device */
707
 
                                request_packet_async(ir, ir->usb_ep_out,
708
 
                                                     cmdbuf, sizeof(cmdbuf),
709
 
                                                     PHILUSB_OUTBOUND);
710
 
                                return carrier;
711
 
                        }
712
 
                }
713
 
 
714
 
                return -EINVAL;
715
 
 
716
 
        }
717
 
 
718
 
        return carrier;
719
 
}
720
 
 
721
 
 
722
 
static int lirc_ioctl(struct inode *node, struct file *filep,
723
 
                      unsigned int cmd, unsigned long arg)
724
 
{
725
 
        int result;
726
 
        unsigned int ivalue;
727
 
        unsigned long lvalue;
728
 
        struct mceusb2_dev *ir = NULL;
729
 
 
730
 
        /* Retrieve lirc_driver data for the device */
731
 
        ir = lirc_get_pdata(filep);
732
 
        if (!ir || !ir->usb_ep_out)
733
 
                return -EFAULT;
734
 
 
735
 
 
736
 
        switch (cmd) {
737
 
        case LIRC_SET_TRANSMITTER_MASK:
738
 
 
739
 
                result = get_user(ivalue, (unsigned int *) arg);
740
 
                if (result)
741
 
                        return result;
742
 
                switch (ivalue) {
743
 
                case 0x01: /* Transmitter 1     => 0x04 */
744
 
                case 0x02: /* Transmitter 2     => 0x02 */
745
 
                case 0x03: /* Transmitter 1 & 2 => 0x06 */
746
 
                        set_transmitter_mask(ir, ivalue);
747
 
                        break;
748
 
 
749
 
                default: /* Unsupported transmitter mask */
750
 
                        return MCE_MAX_CHANNELS;
751
 
                }
752
 
 
753
 
                dprintk(DRIVER_NAME ": SET_TRANSMITTERS mask=%d\n", ivalue);
754
 
                break;
755
 
 
756
 
        case LIRC_GET_SEND_MODE:
757
 
 
758
 
                result = put_user(LIRC_SEND2MODE(LIRC_CAN_SEND_PULSE &
759
 
                                                 LIRC_CAN_SEND_MASK),
760
 
                                  (unsigned long *) arg);
761
 
 
762
 
                if (result)
763
 
                        return result;
764
 
                break;
765
 
 
766
 
        case LIRC_SET_SEND_MODE:
767
 
 
768
 
                result = get_user(lvalue, (unsigned long *) arg);
769
 
 
770
 
                if (result)
771
 
                        return result;
772
 
                if (lvalue != (LIRC_MODE_PULSE&LIRC_CAN_SEND_MASK))
773
 
                        return -EINVAL;
774
 
                break;
775
 
 
776
 
        case LIRC_SET_SEND_CARRIER:
777
 
 
778
 
                result = get_user(ivalue, (unsigned int *) arg);
779
 
                if (result)
780
 
                        return result;
781
 
 
782
 
                set_send_carrier(ir, ivalue);
783
 
                break;
784
 
 
785
 
        default:
786
 
                return -ENOIOCTLCMD;
787
 
        }
788
 
 
789
 
        return 0;
790
 
}
791
 
 
792
 
static struct file_operations lirc_fops = {
793
 
        .owner  = THIS_MODULE,
794
 
        .write  = lirc_write,
795
 
        .ioctl  = lirc_ioctl,
796
 
};
797
 
 
798
 
 
799
 
static int mceusb_dev_probe(struct usb_interface *intf,
800
 
                                const struct usb_device_id *id)
801
 
{
802
 
        struct usb_device *dev = interface_to_usbdev(intf);
803
 
        struct usb_host_interface *idesc;
804
 
        struct usb_endpoint_descriptor *ep = NULL;
805
 
        struct usb_endpoint_descriptor *ep_in = NULL;
806
 
        struct usb_endpoint_descriptor *ep_out = NULL;
807
 
        struct usb_host_config *config;
808
 
        struct mceusb2_dev *ir = NULL;
809
 
        struct lirc_driver *driver = NULL;
810
 
        struct lirc_buffer *rbuf = NULL;
811
 
        int devnum, pipe, maxp;
812
 
        int minor = 0;
813
 
        int i;
814
 
        char buf[63], name[128] = "";
815
 
        int mem_failure = 0;
816
 
        int is_pinnacle;
817
 
 
818
 
        dprintk(DRIVER_NAME ": usb probe called\n");
819
 
 
820
 
        usb_reset_device(dev);
821
 
 
822
 
        config = dev->actconfig;
823
 
 
824
 
        idesc = intf->cur_altsetting;
825
 
 
826
 
        is_pinnacle = usb_match_id(intf, pinnacle_list) ? 1 : 0;
827
 
 
828
 
        /* step through the endpoints to find first bulk in and out endpoint */
829
 
        for (i = 0; i < idesc->desc.bNumEndpoints; ++i) {
830
 
                ep = &idesc->endpoint[i].desc;
831
 
 
832
 
                if ((ep_in == NULL)
833
 
                        && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
834
 
                            == USB_DIR_IN)
835
 
                        && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
836
 
                            == USB_ENDPOINT_XFER_BULK)
837
 
                        || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
838
 
                            == USB_ENDPOINT_XFER_INT))) {
839
 
 
840
 
                        dprintk(DRIVER_NAME ": acceptable inbound endpoint "
841
 
                                "found\n");
842
 
                        ep_in = ep;
843
 
                        ep_in->bmAttributes = USB_ENDPOINT_XFER_INT;
844
 
                        if (is_pinnacle)
845
 
                                /*
846
 
                                 * setting seems to 1 seem to cause issues with
847
 
                                 * Pinnacle timing out on transfer.
848
 
                                 */
849
 
                                ep_in->bInterval = ep->bInterval;
850
 
                        else
851
 
                                ep_in->bInterval = 1;
852
 
                }
853
 
 
854
 
                if ((ep_out == NULL)
855
 
                        && ((ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
856
 
                            == USB_DIR_OUT)
857
 
                        && (((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
858
 
                            == USB_ENDPOINT_XFER_BULK)
859
 
                        || ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
860
 
                            == USB_ENDPOINT_XFER_INT))) {
861
 
 
862
 
                        dprintk(DRIVER_NAME ": acceptable outbound endpoint "
863
 
                                "found\n");
864
 
                        ep_out = ep;
865
 
                        ep_out->bmAttributes = USB_ENDPOINT_XFER_INT;
866
 
                        if (is_pinnacle)
867
 
                                /*
868
 
                                 * setting seems to 1 seem to cause issues with
869
 
                                 * Pinnacle timing out on transfer.
870
 
                                 */
871
 
                                ep_out->bInterval = ep->bInterval;
872
 
                        else
873
 
                                ep_out->bInterval = 1;
874
 
                }
875
 
        }
876
 
        if (ep_in == NULL || ep_out == NULL) {
877
 
                dprintk(DRIVER_NAME ": inbound and/or "
878
 
                        "outbound endpoint not found\n");
879
 
                return -ENODEV;
880
 
        }
881
 
 
882
 
        devnum = dev->devnum;
883
 
        pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
884
 
        maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
885
 
 
886
 
        mem_failure = 0;
887
 
        ir = kzalloc(sizeof(struct mceusb2_dev), GFP_KERNEL);
888
 
        if (!ir) {
889
 
                mem_failure = 1;
890
 
                goto mem_failure_switch;
891
 
        }
892
 
 
893
 
        driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
894
 
        if (!driver) {
895
 
                mem_failure = 2;
896
 
                goto mem_failure_switch;
897
 
        }
898
 
 
899
 
        rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
900
 
        if (!rbuf) {
901
 
                mem_failure = 3;
902
 
                goto mem_failure_switch;
903
 
        }
904
 
 
905
 
        if (lirc_buffer_init(rbuf, sizeof(lirc_t), LIRCBUF_SIZE)) {
906
 
                mem_failure = 4;
907
 
                goto mem_failure_switch;
908
 
        }
909
 
 
910
 
        ir->buf_in = usb_buffer_alloc(dev, maxp, GFP_ATOMIC, &ir->dma_in);
911
 
        if (!ir->buf_in) {
912
 
                mem_failure = 5;
913
 
                goto mem_failure_switch;
914
 
        }
915
 
 
916
 
        ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
917
 
        if (!ir->urb_in) {
918
 
                mem_failure = 7;
919
 
                goto mem_failure_switch;
920
 
        }
921
 
 
922
 
        strcpy(driver->name, DRIVER_NAME " ");
923
 
        driver->minor = -1;
924
 
        driver->features = LIRC_CAN_SEND_PULSE |
925
 
                LIRC_CAN_SET_TRANSMITTER_MASK |
926
 
                LIRC_CAN_REC_MODE2 |
927
 
                LIRC_CAN_SET_SEND_CARRIER;
928
 
        driver->data = ir;
929
 
        driver->rbuf = rbuf;
930
 
        driver->set_use_inc = &set_use_inc;
931
 
        driver->set_use_dec = &set_use_dec;
932
 
        driver->code_length = sizeof(lirc_t) * 8;
933
 
        driver->fops  = &lirc_fops;
934
 
        driver->dev   = &intf->dev;
935
 
        driver->owner = THIS_MODULE;
936
 
 
937
 
        mutex_init(&ir->lock);
938
 
        init_waitqueue_head(&ir->wait_out);
939
 
 
940
 
        minor = lirc_register_driver(driver);
941
 
        if (minor < 0)
942
 
                mem_failure = 9;
943
 
 
944
 
mem_failure_switch:
945
 
 
946
 
        switch (mem_failure) {
947
 
        case 9:
948
 
                usb_free_urb(ir->urb_in);
949
 
        case 7:
950
 
                usb_buffer_free(dev, maxp, ir->buf_in, ir->dma_in);
951
 
        case 5:
952
 
                lirc_buffer_free(rbuf);
953
 
        case 4:
954
 
                kfree(rbuf);
955
 
        case 3:
956
 
                kfree(driver);
957
 
        case 2:
958
 
                kfree(ir);
959
 
        case 1:
960
 
                printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n",
961
 
                        devnum, mem_failure);
962
 
                return -ENOMEM;
963
 
        }
964
 
 
965
 
        driver->minor = minor;
966
 
        ir->d = driver;
967
 
        ir->devnum = devnum;
968
 
        ir->usbdev = dev;
969
 
        ir->len_in = maxp;
970
 
        ir->flags.connected = 0;
971
 
        ir->flags.pinnacle = is_pinnacle;
972
 
        ir->flags.transmitter_mask_inverted =
973
 
                usb_match_id(intf, transmitter_mask_list) ? 0 : 1;
974
 
 
975
 
        ir->lircdata = PULSE_MASK;
976
 
        ir->is_pulse = 0;
977
 
 
978
 
        /* ir->flags.transmitter_mask_inverted must be set */
979
 
        set_transmitter_mask(ir, MCE_DEFAULT_TX_MASK);
980
 
        /* Saving usb interface data for use by the transmitter routine */
981
 
        ir->usb_ep_in = ep_in;
982
 
        ir->usb_ep_out = ep_out;
983
 
 
984
 
        if (dev->descriptor.iManufacturer
985
 
            && usb_string(dev, dev->descriptor.iManufacturer,
986
 
                          buf, sizeof(buf)) > 0)
987
 
                strlcpy(name, buf, sizeof(name));
988
 
        if (dev->descriptor.iProduct
989
 
            && usb_string(dev, dev->descriptor.iProduct,
990
 
                          buf, sizeof(buf)) > 0)
991
 
                snprintf(name + strlen(name), sizeof(name) - strlen(name),
992
 
                         " %s", buf);
993
 
        printk(DRIVER_NAME "[%d]: %s on usb%d:%d\n", devnum, name,
994
 
               dev->bus->busnum, devnum);
995
 
 
996
 
        /* inbound data */
997
 
        usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
998
 
                maxp, (usb_complete_t) mceusb_dev_recv, ir, ep_in->bInterval);
999
 
        ir->urb_in->transfer_dma = ir->dma_in;
1000
 
        ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1001
 
 
1002
 
        /* initialize device */
1003
 
        if (ir->flags.pinnacle) {
1004
 
                int usbret;
1005
 
 
1006
 
                /*
1007
 
                 * I have no idea why but this reset seems to be crucial to
1008
 
                 * getting the device to do outbound IO correctly - without
1009
 
                 * this the device seems to hang, ignoring all input - although
1010
 
                 * IR signals are correctly sent from the device, no input is
1011
 
                 * interpreted by the device and the host never does the
1012
 
                 * completion routine
1013
 
                 */
1014
 
 
1015
 
                usbret = usb_reset_configuration(dev);
1016
 
                printk(DRIVER_NAME "[%d]: usb reset config ret %x\n",
1017
 
                       devnum, usbret);
1018
 
 
1019
 
                /*
1020
 
                 * its possible we really should wait for a return
1021
 
                 * for each of these...
1022
 
                 */
1023
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1024
 
                request_packet_async(ir, ep_out, pin_init1, sizeof(pin_init1),
1025
 
                                     PHILUSB_OUTBOUND);
1026
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1027
 
                request_packet_async(ir, ep_out, pin_init2, sizeof(pin_init2),
1028
 
                                     PHILUSB_OUTBOUND);
1029
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1030
 
                request_packet_async(ir, ep_out, pin_init3, sizeof(pin_init3),
1031
 
                                     PHILUSB_OUTBOUND);
1032
 
                /* if we don't issue the correct number of receives
1033
 
                 * (PHILUSB_INBOUND) for each outbound, then the first few ir
1034
 
                 * pulses will be interpreted by the usb_async_callback routine
1035
 
                 * - we should ensure we have the right amount OR less - as the
1036
 
                 * mceusb_dev_recv routine will handle the control packets OK -
1037
 
                 * they start with 0x9f - but the async callback doesn't handle
1038
 
                 * ir pulse packets
1039
 
                 */
1040
 
                request_packet_async(ir, ep_in, NULL, maxp, 0);
1041
 
        } else {
1042
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1043
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1044
 
                request_packet_async(ir, ep_out, init1,
1045
 
                                     sizeof(init1), PHILUSB_OUTBOUND);
1046
 
                request_packet_async(ir, ep_in, NULL, maxp, PHILUSB_INBOUND);
1047
 
                request_packet_async(ir, ep_out, init2,
1048
 
                                     sizeof(init2), PHILUSB_OUTBOUND);
1049
 
                request_packet_async(ir, ep_in, NULL, maxp, 0);
1050
 
        }
1051
 
 
1052
 
        usb_set_intfdata(intf, ir);
1053
 
 
1054
 
        return 0;
1055
 
}
1056
 
 
1057
 
 
1058
 
static void mceusb_dev_disconnect(struct usb_interface *intf)
1059
 
{
1060
 
        struct usb_device *dev = interface_to_usbdev(intf);
1061
 
        struct mceusb2_dev *ir = usb_get_intfdata(intf);
1062
 
 
1063
 
        usb_set_intfdata(intf, NULL);
1064
 
 
1065
 
        if (!ir || !ir->d)
1066
 
                return;
1067
 
 
1068
 
        ir->usbdev = NULL;
1069
 
        wake_up_all(&ir->wait_out);
1070
 
 
1071
 
        mutex_lock(&ir->lock);
1072
 
        usb_kill_urb(ir->urb_in);
1073
 
        usb_free_urb(ir->urb_in);
1074
 
        usb_buffer_free(dev, ir->len_in, ir->buf_in, ir->dma_in);
1075
 
        mutex_unlock(&ir->lock);
1076
 
 
1077
 
        unregister_from_lirc(ir);
1078
 
}
1079
 
 
1080
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1081
 
static int mceusb_dev_suspend(struct usb_interface *intf, pm_message_t message)
1082
 
{
1083
 
        struct mceusb2_dev *ir = usb_get_intfdata(intf);
1084
 
        printk(DRIVER_NAME "[%d]: suspend\n", ir->devnum);
1085
 
        usb_kill_urb(ir->urb_in);
1086
 
        return 0;
1087
 
}
1088
 
 
1089
 
static int mceusb_dev_resume(struct usb_interface *intf)
1090
 
{
1091
 
        struct mceusb2_dev *ir = usb_get_intfdata(intf);
1092
 
        printk(DRIVER_NAME "[%d]: resume\n", ir->devnum);
1093
 
        if (usb_submit_urb(ir->urb_in, GFP_ATOMIC))
1094
 
                return -EIO;
1095
 
        return 0;
1096
 
}
1097
 
#endif
1098
 
 
1099
 
static struct usb_driver mceusb_dev_driver = {
1100
 
        LIRC_THIS_MODULE(.owner = THIS_MODULE)
1101
 
        .name =         DRIVER_NAME,
1102
 
        .probe =        mceusb_dev_probe,
1103
 
        .disconnect =   mceusb_dev_disconnect,
1104
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
1105
 
        .suspend =      mceusb_dev_suspend,
1106
 
        .resume =       mceusb_dev_resume,
1107
 
#endif
1108
 
        .id_table =     mceusb_dev_table
1109
 
};
1110
 
 
1111
 
static int __init mceusb_dev_init(void)
1112
 
{
1113
 
        int i;
1114
 
 
1115
 
        printk(KERN_INFO "\n");
1116
 
        printk(KERN_INFO DRIVER_NAME ": " DRIVER_DESC " " DRIVER_VERSION "\n");
1117
 
        printk(KERN_INFO DRIVER_NAME ": " DRIVER_AUTHOR "\n");
1118
 
        dprintk(DRIVER_NAME ": debug mode enabled\n");
1119
 
 
1120
 
        i = usb_register(&mceusb_dev_driver);
1121
 
        if (i < 0) {
1122
 
                printk(DRIVER_NAME ": usb register failed, result = %d\n", i);
1123
 
                return -ENODEV;
1124
 
        }
1125
 
 
1126
 
        return 0;
1127
 
}
1128
 
 
1129
 
static void __exit mceusb_dev_exit(void)
1130
 
{
1131
 
        usb_deregister(&mceusb_dev_driver);
1132
 
}
1133
 
 
1134
 
module_init(mceusb_dev_init);
1135
 
module_exit(mceusb_dev_exit);
1136
 
 
1137
 
MODULE_DESCRIPTION(DRIVER_DESC);
1138
 
MODULE_AUTHOR(DRIVER_AUTHOR);
1139
 
MODULE_LICENSE("GPL");
1140
 
MODULE_DEVICE_TABLE(usb, mceusb_dev_table);
1141
 
 
1142
 
module_param(debug, bool, S_IRUGO | S_IWUSR);
1143
 
MODULE_PARM_DESC(debug, "Debug enabled or not");
1144
 
 
1145
 
EXPORT_NO_SYMBOLS;