1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include <linux/kernel.h>
54#include <linux/errno.h>
55#include <linux/init.h>
56#include <linux/slab.h>
57#include <linux/tty.h>
58#include <linux/tty_driver.h>
59#include <linux/tty_flip.h>
60#include <linux/module.h>
61#include <linux/spinlock.h>
62#include <asm/uaccess.h>
63#include <linux/usb.h>
64#include <linux/usb/serial.h>
65
66
67
68
69#define DRIVER_VERSION "v0.4"
70#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
71#define DRIVER_DESC "USB IR Dongle driver"
72
73
74#define USB_CLASS_IRDA 0x02
75#define USB_DT_IRDA 0x21
76#define IU_REQ_GET_CLASS_DESC 0x06
77#define SPEED_2400 0x01
78#define SPEED_9600 0x02
79#define SPEED_19200 0x03
80#define SPEED_38400 0x04
81#define SPEED_57600 0x05
82#define SPEED_115200 0x06
83#define SPEED_576000 0x07
84#define SPEED_1152000 0x08
85#define SPEED_4000000 0x09
86
87struct irda_class_desc {
88 u8 bLength;
89 u8 bDescriptorType;
90 u16 bcdSpecRevision;
91 u8 bmDataSize;
92 u8 bmWindowSize;
93 u8 bmMinTurnaroundTime;
94 u16 wBaudRate;
95 u8 bmAdditionalBOFs;
96 u8 bIrdaRateSniff;
97 u8 bMaxUnicastList;
98} __attribute__ ((packed));
99
100static int debug;
101
102
103
104static int buffer_size;
105
106static int xbof = -1;
107
108static int ir_startup (struct usb_serial *serial);
109static int ir_open (struct usb_serial_port *port, struct file *filep);
110static void ir_close (struct usb_serial_port *port, struct file *filep);
111static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count);
112static void ir_write_bulk_callback (struct urb *urb);
113static void ir_read_bulk_callback (struct urb *urb);
114static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios);
115
116
117static u8 ir_baud = 0;
118static u8 ir_xbof = 0;
119static u8 ir_add_bof = 0;
120
121static struct usb_device_id id_table [] = {
122 { USB_DEVICE(0x050f, 0x0180) },
123 { USB_DEVICE(0x08e9, 0x0100) },
124 { USB_DEVICE(0x09c4, 0x0011) },
125 { USB_INTERFACE_INFO (USB_CLASS_APP_SPEC, USB_CLASS_IRDA, 0) },
126 { }
127};
128
129MODULE_DEVICE_TABLE (usb, id_table);
130
131static struct usb_driver ir_driver = {
132 .name = "ir-usb",
133 .probe = usb_serial_probe,
134 .disconnect = usb_serial_disconnect,
135 .id_table = id_table,
136 .no_dynamic_id = 1,
137};
138
139
140static struct usb_serial_driver ir_device = {
141 .driver = {
142 .owner = THIS_MODULE,
143 .name = "ir-usb",
144 },
145 .description = "IR Dongle",
146 .usb_driver = &ir_driver,
147 .id_table = id_table,
148 .num_interrupt_in = 1,
149 .num_bulk_in = 1,
150 .num_bulk_out = 1,
151 .num_ports = 1,
152 .set_termios = ir_set_termios,
153 .attach = ir_startup,
154 .open = ir_open,
155 .close = ir_close,
156 .write = ir_write,
157 .write_bulk_callback = ir_write_bulk_callback,
158 .read_bulk_callback = ir_read_bulk_callback,
159};
160
161static inline void irda_usb_dump_class_desc(struct irda_class_desc *desc)
162{
163 dbg("bLength=%x", desc->bLength);
164 dbg("bDescriptorType=%x", desc->bDescriptorType);
165 dbg("bcdSpecRevision=%x", desc->bcdSpecRevision);
166 dbg("bmDataSize=%x", desc->bmDataSize);
167 dbg("bmWindowSize=%x", desc->bmWindowSize);
168 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
169 dbg("wBaudRate=%x", desc->wBaudRate);
170 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
171 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
172 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
173}
174
175
176
177
178
179
180
181
182
183
184
185
186
187static struct irda_class_desc *irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
188{
189 struct irda_class_desc *desc;
190 int ret;
191
192 desc = kzalloc(sizeof (struct irda_class_desc), GFP_KERNEL);
193 if (desc == NULL)
194 return NULL;
195
196 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev,0),
197 IU_REQ_GET_CLASS_DESC,
198 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
199 0, ifnum, desc, sizeof(*desc), 1000);
200
201 dbg("%s - ret=%d", __FUNCTION__, ret);
202 if (ret < sizeof(*desc)) {
203 dbg("%s - class descriptor read %s (%d)",
204 __FUNCTION__,
205 (ret<0) ? "failed" : "too short",
206 ret);
207 goto error;
208 }
209 if (desc->bDescriptorType != USB_DT_IRDA) {
210 dbg("%s - bad class descriptor type", __FUNCTION__);
211 goto error;
212 }
213
214 irda_usb_dump_class_desc(desc);
215 return desc;
216error:
217 kfree(desc);
218 return NULL;
219}
220
221
222static u8 ir_xbof_change(u8 xbof)
223{
224 u8 result;
225
226 switch(xbof) {
227 case 48: result = 0x10; break;
228 case 28:
229 case 24: result = 0x20; break;
230 default:
231 case 12: result = 0x30; break;
232 case 5:
233 case 6: result = 0x40; break;
234 case 3: result = 0x50; break;
235 case 2: result = 0x60; break;
236 case 1: result = 0x70; break;
237 case 0: result = 0x80; break;
238 }
239 return(result);
240}
241
242
243static int ir_startup (struct usb_serial *serial)
244{
245 struct irda_class_desc *irda_desc;
246
247 irda_desc = irda_usb_find_class_desc (serial->dev, 0);
248 if (irda_desc == NULL) {
249 dev_err (&serial->dev->dev, "IRDA class descriptor not found, device not bound\n");
250 return -ENODEV;
251 }
252
253 dbg ("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
254 __FUNCTION__,
255 (irda_desc->wBaudRate & 0x0001) ? " 2400" : "",
256 (irda_desc->wBaudRate & 0x0002) ? " 9600" : "",
257 (irda_desc->wBaudRate & 0x0004) ? " 19200" : "",
258 (irda_desc->wBaudRate & 0x0008) ? " 38400" : "",
259 (irda_desc->wBaudRate & 0x0010) ? " 57600" : "",
260 (irda_desc->wBaudRate & 0x0020) ? " 115200" : "",
261 (irda_desc->wBaudRate & 0x0040) ? " 576000" : "",
262 (irda_desc->wBaudRate & 0x0080) ? " 1152000" : "",
263 (irda_desc->wBaudRate & 0x0100) ? " 4000000" : "");
264
265 switch( irda_desc->bmAdditionalBOFs ) {
266 case 0x01: ir_add_bof = 48; break;
267 case 0x02: ir_add_bof = 24; break;
268 case 0x04: ir_add_bof = 12; break;
269 case 0x08: ir_add_bof = 6; break;
270 case 0x10: ir_add_bof = 3; break;
271 case 0x20: ir_add_bof = 2; break;
272 case 0x40: ir_add_bof = 1; break;
273 case 0x80: ir_add_bof = 0; break;
274 default:;
275 }
276
277 kfree (irda_desc);
278
279 return 0;
280}
281
282static int ir_open (struct usb_serial_port *port, struct file *filp)
283{
284 char *buffer;
285 int result = 0;
286
287 dbg("%s - port %d", __FUNCTION__, port->number);
288
289 if (buffer_size) {
290
291 buffer = kmalloc (buffer_size, GFP_KERNEL);
292 if (!buffer) {
293 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
294 return -ENOMEM;
295 }
296 kfree (port->read_urb->transfer_buffer);
297 port->read_urb->transfer_buffer = buffer;
298 port->read_urb->transfer_buffer_length = buffer_size;
299
300 buffer = kmalloc (buffer_size, GFP_KERNEL);
301 if (!buffer) {
302 dev_err (&port->dev, "%s - out of memory.\n", __FUNCTION__);
303 return -ENOMEM;
304 }
305 kfree (port->write_urb->transfer_buffer);
306 port->write_urb->transfer_buffer = buffer;
307 port->write_urb->transfer_buffer_length = buffer_size;
308 port->bulk_out_size = buffer_size;
309 }
310
311
312 usb_fill_bulk_urb (
313 port->read_urb,
314 port->serial->dev,
315 usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
316 port->read_urb->transfer_buffer,
317 port->read_urb->transfer_buffer_length,
318 ir_read_bulk_callback,
319 port);
320 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
321 if (result)
322 dev_err(&port->dev, "%s - failed submitting read urb, error %d\n", __FUNCTION__, result);
323
324 return result;
325}
326
327static void ir_close (struct usb_serial_port *port, struct file * filp)
328{
329 dbg("%s - port %d", __FUNCTION__, port->number);
330
331
332 usb_kill_urb(port->read_urb);
333}
334
335static int ir_write (struct usb_serial_port *port, const unsigned char *buf, int count)
336{
337 unsigned char *transfer_buffer;
338 int result;
339 int transfer_size;
340
341 dbg("%s - port = %d, count = %d", __FUNCTION__, port->number, count);
342
343 if (!port->tty) {
344 dev_err (&port->dev, "%s - no tty???\n", __FUNCTION__);
345 return 0;
346 }
347
348 if (count == 0)
349 return 0;
350
351 spin_lock_bh(&port->lock);
352 if (port->write_urb_busy) {
353 spin_unlock_bh(&port->lock);
354 dbg("%s - already writing", __FUNCTION__);
355 return 0;
356 }
357 port->write_urb_busy = 1;
358 spin_unlock_bh(&port->lock);
359
360 transfer_buffer = port->write_urb->transfer_buffer;
361 transfer_size = min(count, port->bulk_out_size - 1);
362
363
364
365
366
367
368
369
370 *transfer_buffer = ir_xbof | ir_baud;
371 ++transfer_buffer;
372
373 memcpy (transfer_buffer, buf, transfer_size);
374
375 usb_fill_bulk_urb (
376 port->write_urb,
377 port->serial->dev,
378 usb_sndbulkpipe(port->serial->dev,
379 port->bulk_out_endpointAddress),
380 port->write_urb->transfer_buffer,
381 transfer_size + 1,
382 ir_write_bulk_callback,
383 port);
384
385 port->write_urb->transfer_flags = URB_ZERO_PACKET;
386
387 result = usb_submit_urb (port->write_urb, GFP_ATOMIC);
388 if (result) {
389 port->write_urb_busy = 0;
390 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
391 } else
392 result = transfer_size;
393
394 return result;
395}
396
397static void ir_write_bulk_callback (struct urb *urb)
398{
399 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
400 int status = urb->status;
401
402 dbg("%s - port %d", __FUNCTION__, port->number);
403
404 port->write_urb_busy = 0;
405 if (status) {
406 dbg("%s - nonzero write bulk status received: %d",
407 __FUNCTION__, status);
408 return;
409 }
410
411 usb_serial_debug_data (
412 debug,
413 &port->dev,
414 __FUNCTION__,
415 urb->actual_length,
416 urb->transfer_buffer);
417
418 usb_serial_port_softint(port);
419}
420
421static void ir_read_bulk_callback (struct urb *urb)
422{
423 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
424 struct tty_struct *tty;
425 unsigned char *data = urb->transfer_buffer;
426 int result;
427 int status = urb->status;
428
429 dbg("%s - port %d", __FUNCTION__, port->number);
430
431 if (!port->open_count) {
432 dbg("%s - port closed.", __FUNCTION__);
433 return;
434 }
435
436 switch (status) {
437 case 0:
438
439
440
441
442
443
444 if ((*data & 0x0f) > 0)
445 ir_baud = *data & 0x0f;
446
447 usb_serial_debug_data (
448 debug,
449 &port->dev,
450 __FUNCTION__,
451 urb->actual_length,
452 data);
453
454 tty = port->tty;
455
456 if (tty_buffer_request_room(tty, urb->actual_length - 1)) {
457 tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
458 tty_flip_buffer_push(tty);
459 }
460
461
462
463
464
465
466
467 case -EPROTO:
468
469
470 usb_fill_bulk_urb (
471 port->read_urb,
472 port->serial->dev,
473 usb_rcvbulkpipe(port->serial->dev,
474 port->bulk_in_endpointAddress),
475 port->read_urb->transfer_buffer,
476 port->read_urb->transfer_buffer_length,
477 ir_read_bulk_callback,
478 port);
479
480 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
481 if (result)
482 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
483 __FUNCTION__, result);
484
485 break ;
486
487 default:
488 dbg("%s - nonzero read bulk status received: %d",
489 __FUNCTION__,
490 status);
491 break ;
492
493 }
494
495 return;
496}
497
498static void ir_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
499{
500 unsigned char *transfer_buffer;
501 int result;
502 speed_t baud;
503 int ir_baud;
504
505 dbg("%s - port %d", __FUNCTION__, port->number);
506
507 baud = tty_get_baud_rate(port->tty);
508
509
510
511
512
513
514
515 switch (baud) {
516 case 2400: ir_baud = SPEED_2400; break;
517 case 9600: ir_baud = SPEED_9600; break;
518 case 19200: ir_baud = SPEED_19200; break;
519 case 38400: ir_baud = SPEED_38400; break;
520 case 57600: ir_baud = SPEED_57600; break;
521 case 115200: ir_baud = SPEED_115200; break;
522 case 576000: ir_baud = SPEED_576000; break;
523 case 1152000: ir_baud = SPEED_1152000; break;
524 case 4000000: ir_baud = SPEED_4000000; break;
525 break;
526 default:
527 ir_baud = SPEED_9600;
528 baud = 9600;
529 }
530
531 if (xbof == -1)
532 ir_xbof = ir_xbof_change(ir_add_bof);
533 else
534 ir_xbof = ir_xbof_change(xbof) ;
535
536
537
538
539
540
541 transfer_buffer = port->write_urb->transfer_buffer;
542 *transfer_buffer = ir_xbof | ir_baud;
543
544 usb_fill_bulk_urb (
545 port->write_urb,
546 port->serial->dev,
547 usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
548 port->write_urb->transfer_buffer,
549 1,
550 ir_write_bulk_callback,
551 port);
552
553 port->write_urb->transfer_flags = URB_ZERO_PACKET;
554
555 result = usb_submit_urb (port->write_urb, GFP_KERNEL);
556 if (result)
557 dev_err(&port->dev, "%s - failed submitting write urb, error %d\n", __FUNCTION__, result);
558
559
560 tty_termios_copy_hw(port->tty->termios, old_termios);
561 tty_encode_baud_rate(port->tty, baud, baud);
562}
563
564
565static int __init ir_init (void)
566{
567 int retval;
568 retval = usb_serial_register(&ir_device);
569 if (retval)
570 goto failed_usb_serial_register;
571 retval = usb_register(&ir_driver);
572 if (retval)
573 goto failed_usb_register;
574 info(DRIVER_DESC " " DRIVER_VERSION);
575 return 0;
576failed_usb_register:
577 usb_serial_deregister(&ir_device);
578failed_usb_serial_register:
579 return retval;
580}
581
582
583static void __exit ir_exit (void)
584{
585 usb_deregister (&ir_driver);
586 usb_serial_deregister (&ir_device);
587}
588
589
590module_init(ir_init);
591module_exit(ir_exit);
592
593MODULE_AUTHOR(DRIVER_AUTHOR);
594MODULE_DESCRIPTION(DRIVER_DESC);
595MODULE_LICENSE("GPL");
596
597module_param(debug, bool, S_IRUGO | S_IWUSR);
598MODULE_PARM_DESC(debug, "Debug enabled or not");
599module_param(xbof, int, 0);
600MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
601module_param(buffer_size, int, 0);
602MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
603
604