| 1 | /* |
| 2 | * lirc_imon.c: LIRC/VFD/LCD driver for SoundGraph iMON IR/VFD/LCD |
| 3 | * including the iMON PAD model |
| 4 | * |
| 5 | * Copyright(C) 2004 Venky Raju(dev@venky.ws) |
| 6 | * Copyright(C) 2009 Jarod Wilson <jarod@wilsonet.com> |
| 7 | * |
| 8 | * lirc_imon is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/usb.h> |
| 30 | |
| 31 | #include <media/lirc.h> |
| 32 | #include <media/lirc_dev.h> |
| 33 | |
| 34 | |
| 35 | #define MOD_AUTHOR "Venky Raju <dev@venky.ws>" |
| 36 | #define MOD_DESC "Driver for SoundGraph iMON MultiMedia IR/Display" |
| 37 | #define MOD_NAME "lirc_imon" |
| 38 | #define MOD_VERSION "0.8" |
| 39 | |
| 40 | #define DISPLAY_MINOR_BASE 144 |
| 41 | #define DEVICE_NAME "lcd%d" |
| 42 | |
| 43 | #define BUF_CHUNK_SIZE 4 |
| 44 | #define BUF_SIZE 128 |
| 45 | |
| 46 | #define BIT_DURATION 250 /* each bit received is 250us */ |
| 47 | |
| 48 | /*** P R O T O T Y P E S ***/ |
| 49 | |
| 50 | /* USB Callback prototypes */ |
| 51 | static int imon_probe(struct usb_interface *interface, |
| 52 | const struct usb_device_id *id); |
| 53 | static void imon_disconnect(struct usb_interface *interface); |
| 54 | static void usb_rx_callback(struct urb *urb); |
| 55 | static void usb_tx_callback(struct urb *urb); |
| 56 | |
| 57 | /* suspend/resume support */ |
| 58 | static int imon_resume(struct usb_interface *intf); |
| 59 | static int imon_suspend(struct usb_interface *intf, pm_message_t message); |
| 60 | |
| 61 | /* Display file_operations function prototypes */ |
| 62 | static int display_open(struct inode *inode, struct file *file); |
| 63 | static int display_close(struct inode *inode, struct file *file); |
| 64 | |
| 65 | /* VFD write operation */ |
| 66 | static ssize_t vfd_write(struct file *file, const char *buf, |
| 67 | size_t n_bytes, loff_t *pos); |
| 68 | |
| 69 | /* LIRC driver function prototypes */ |
| 70 | static int ir_open(void *data); |
| 71 | static void ir_close(void *data); |
| 72 | |
| 73 | /* Driver init/exit prototypes */ |
| 74 | static int __init imon_init(void); |
| 75 | static void __exit imon_exit(void); |
| 76 | |
| 77 | /*** G L O B A L S ***/ |
| 78 | #define IMON_DATA_BUF_SZ 35 |
| 79 | |
| 80 | struct imon_context { |
| 81 | struct usb_device *usbdev; |
| 82 | /* Newer devices have two interfaces */ |
| 83 | int display; /* not all controllers do */ |
| 84 | int display_isopen; /* display port has been opened */ |
| 85 | int ir_isopen; /* IR port open */ |
| 86 | int dev_present; /* USB device presence */ |
| 87 | struct mutex ctx_lock; /* to lock this object */ |
| 88 | wait_queue_head_t remove_ok; /* For unexpected USB disconnects */ |
| 89 | |
| 90 | int vfd_proto_6p; /* some VFD require a 6th packet */ |
| 91 | |
| 92 | struct lirc_driver *driver; |
| 93 | struct usb_endpoint_descriptor *rx_endpoint; |
| 94 | struct usb_endpoint_descriptor *tx_endpoint; |
| 95 | struct urb *rx_urb; |
| 96 | struct urb *tx_urb; |
| 97 | unsigned char usb_rx_buf[8]; |
| 98 | unsigned char usb_tx_buf[8]; |
| 99 | |
| 100 | struct rx_data { |
| 101 | int count; /* length of 0 or 1 sequence */ |
| 102 | int prev_bit; /* logic level of sequence */ |
| 103 | int initial_space; /* initial space flag */ |
| 104 | } rx; |
| 105 | |
| 106 | struct tx_t { |
| 107 | unsigned char data_buf[IMON_DATA_BUF_SZ]; /* user data buffer */ |
| 108 | struct completion finished; /* wait for write to finish */ |
| 109 | atomic_t busy; /* write in progress */ |
| 110 | int status; /* status of tx completion */ |
| 111 | } tx; |
| 112 | }; |
| 113 | |
| 114 | static const struct file_operations display_fops = { |
| 115 | .owner = THIS_MODULE, |
| 116 | .open = &display_open, |
| 117 | .write = &vfd_write, |
| 118 | .release = &display_close, |
| 119 | .llseek = noop_llseek, |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * USB Device ID for iMON USB Control Boards |
| 124 | * |
| 125 | * The Windows drivers contain 6 different inf files, more or less one for |
| 126 | * each new device until the 0x0034-0x0046 devices, which all use the same |
| 127 | * driver. Some of the devices in the 34-46 range haven't been definitively |
| 128 | * identified yet. Early devices have either a TriGem Computer, Inc. or a |
| 129 | * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later |
| 130 | * devices use the SoundGraph vendor ID (0x15c2). |
| 131 | */ |
| 132 | static struct usb_device_id imon_usb_id_table[] = { |
| 133 | /* TriGem iMON (IR only) -- TG_iMON.inf */ |
| 134 | { USB_DEVICE(0x0aa8, 0x8001) }, |
| 135 | |
| 136 | /* SoundGraph iMON (IR only) -- sg_imon.inf */ |
| 137 | { USB_DEVICE(0x04e8, 0xff30) }, |
| 138 | |
| 139 | /* SoundGraph iMON VFD (IR & VFD) -- iMON_VFD.inf */ |
| 140 | { USB_DEVICE(0x0aa8, 0xffda) }, |
| 141 | |
| 142 | /* SoundGraph iMON SS (IR & VFD) -- iMON_SS.inf */ |
| 143 | { USB_DEVICE(0x15c2, 0xffda) }, |
| 144 | |
| 145 | {} |
| 146 | }; |
| 147 | |
| 148 | /* Some iMON VFD models requires a 6th packet for VFD writes */ |
| 149 | static struct usb_device_id vfd_proto_6p_list[] = { |
| 150 | { USB_DEVICE(0x15c2, 0xffda) }, |
| 151 | {} |
| 152 | }; |
| 153 | |
| 154 | /* Some iMON devices have no lcd/vfd, don't set one up */ |
| 155 | static struct usb_device_id ir_only_list[] = { |
| 156 | { USB_DEVICE(0x0aa8, 0x8001) }, |
| 157 | { USB_DEVICE(0x04e8, 0xff30) }, |
| 158 | {} |
| 159 | }; |
| 160 | |
| 161 | /* USB Device data */ |
| 162 | static struct usb_driver imon_driver = { |
| 163 | .name = MOD_NAME, |
| 164 | .probe = imon_probe, |
| 165 | .disconnect = imon_disconnect, |
| 166 | .suspend = imon_suspend, |
| 167 | .resume = imon_resume, |
| 168 | .id_table = imon_usb_id_table, |
| 169 | }; |
| 170 | |
| 171 | static struct usb_class_driver imon_class = { |
| 172 | .name = DEVICE_NAME, |
| 173 | .fops = &display_fops, |
| 174 | .minor_base = DISPLAY_MINOR_BASE, |
| 175 | }; |
| 176 | |
| 177 | /* to prevent races between open() and disconnect(), probing, etc */ |
| 178 | static DEFINE_MUTEX(driver_lock); |
| 179 | |
| 180 | static int debug; |
| 181 | |
| 182 | /*** M O D U L E C O D E ***/ |
| 183 | |
| 184 | MODULE_AUTHOR(MOD_AUTHOR); |
| 185 | MODULE_DESCRIPTION(MOD_DESC); |
| 186 | MODULE_VERSION(MOD_VERSION); |
| 187 | MODULE_LICENSE("GPL"); |
| 188 | MODULE_DEVICE_TABLE(usb, imon_usb_id_table); |
| 189 | module_param(debug, int, S_IRUGO | S_IWUSR); |
| 190 | MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes(default: no)"); |
| 191 | |
| 192 | static void free_imon_context(struct imon_context *context) |
| 193 | { |
| 194 | struct device *dev = context->driver->dev; |
| 195 | usb_free_urb(context->tx_urb); |
| 196 | usb_free_urb(context->rx_urb); |
| 197 | lirc_buffer_free(context->driver->rbuf); |
| 198 | kfree(context->driver->rbuf); |
| 199 | kfree(context->driver); |
| 200 | kfree(context); |
| 201 | |
| 202 | dev_dbg(dev, "%s: iMON context freed\n", __func__); |
| 203 | } |
| 204 | |
| 205 | static void deregister_from_lirc(struct imon_context *context) |
| 206 | { |
| 207 | int retval; |
| 208 | int minor = context->driver->minor; |
| 209 | |
| 210 | retval = lirc_unregister_driver(minor); |
| 211 | if (retval) |
| 212 | err("%s: unable to deregister from lirc(%d)", |
| 213 | __func__, retval); |
| 214 | else |
| 215 | printk(KERN_INFO MOD_NAME ": Deregistered iMON driver " |
| 216 | "(minor:%d)\n", minor); |
| 217 | |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Called when the Display device (e.g. /dev/lcd0) |
| 222 | * is opened by the application. |
| 223 | */ |
| 224 | static int display_open(struct inode *inode, struct file *file) |
| 225 | { |
| 226 | struct usb_interface *interface; |
| 227 | struct imon_context *context = NULL; |
| 228 | int subminor; |
| 229 | int retval = 0; |
| 230 | |
| 231 | /* prevent races with disconnect */ |
| 232 | mutex_lock(&driver_lock); |
| 233 | |
| 234 | subminor = iminor(inode); |
| 235 | interface = usb_find_interface(&imon_driver, subminor); |
| 236 | if (!interface) { |
| 237 | err("%s: could not find interface for minor %d", |
| 238 | __func__, subminor); |
| 239 | retval = -ENODEV; |
| 240 | goto exit; |
| 241 | } |
| 242 | context = usb_get_intfdata(interface); |
| 243 | |
| 244 | if (!context) { |
| 245 | err("%s: no context found for minor %d", |
| 246 | __func__, subminor); |
| 247 | retval = -ENODEV; |
| 248 | goto exit; |
| 249 | } |
| 250 | |
| 251 | mutex_lock(&context->ctx_lock); |
| 252 | |
| 253 | if (!context->display) { |
| 254 | err("%s: display not supported by device", __func__); |
| 255 | retval = -ENODEV; |
| 256 | } else if (context->display_isopen) { |
| 257 | err("%s: display port is already open", __func__); |
| 258 | retval = -EBUSY; |
| 259 | } else { |
| 260 | context->display_isopen = 1; |
| 261 | file->private_data = context; |
| 262 | dev_info(context->driver->dev, "display port opened\n"); |
| 263 | } |
| 264 | |
| 265 | mutex_unlock(&context->ctx_lock); |
| 266 | |
| 267 | exit: |
| 268 | mutex_unlock(&driver_lock); |
| 269 | return retval; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Called when the display device (e.g. /dev/lcd0) |
| 274 | * is closed by the application. |
| 275 | */ |
| 276 | static int display_close(struct inode *inode, struct file *file) |
| 277 | { |
| 278 | struct imon_context *context = NULL; |
| 279 | int retval = 0; |
| 280 | |
| 281 | context = (struct imon_context *)file->private_data; |
| 282 | |
| 283 | if (!context) { |
| 284 | err("%s: no context for device", __func__); |
| 285 | return -ENODEV; |
| 286 | } |
| 287 | |
| 288 | mutex_lock(&context->ctx_lock); |
| 289 | |
| 290 | if (!context->display) { |
| 291 | err("%s: display not supported by device", __func__); |
| 292 | retval = -ENODEV; |
| 293 | } else if (!context->display_isopen) { |
| 294 | err("%s: display is not open", __func__); |
| 295 | retval = -EIO; |
| 296 | } else { |
| 297 | context->display_isopen = 0; |
| 298 | dev_info(context->driver->dev, "display port closed\n"); |
| 299 | if (!context->dev_present && !context->ir_isopen) { |
| 300 | /* |
| 301 | * Device disconnected before close and IR port is not |
| 302 | * open. If IR port is open, context will be deleted by |
| 303 | * ir_close. |
| 304 | */ |
| 305 | mutex_unlock(&context->ctx_lock); |
| 306 | free_imon_context(context); |
| 307 | return retval; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | mutex_unlock(&context->ctx_lock); |
| 312 | return retval; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Sends a packet to the device -- this function must be called |
| 317 | * with context->ctx_lock held. |
| 318 | */ |
| 319 | static int send_packet(struct imon_context *context) |
| 320 | { |
| 321 | unsigned int pipe; |
| 322 | int interval = 0; |
| 323 | int retval = 0; |
| 324 | struct usb_ctrlrequest *control_req = NULL; |
| 325 | |
| 326 | /* Check if we need to use control or interrupt urb */ |
| 327 | pipe = usb_sndintpipe(context->usbdev, |
| 328 | context->tx_endpoint->bEndpointAddress); |
| 329 | interval = context->tx_endpoint->bInterval; |
| 330 | |
| 331 | usb_fill_int_urb(context->tx_urb, context->usbdev, pipe, |
| 332 | context->usb_tx_buf, |
| 333 | sizeof(context->usb_tx_buf), |
| 334 | usb_tx_callback, context, interval); |
| 335 | |
| 336 | context->tx_urb->actual_length = 0; |
| 337 | |
| 338 | init_completion(&context->tx.finished); |
| 339 | atomic_set(&(context->tx.busy), 1); |
| 340 | |
| 341 | retval = usb_submit_urb(context->tx_urb, GFP_KERNEL); |
| 342 | if (retval) { |
| 343 | atomic_set(&(context->tx.busy), 0); |
| 344 | err("%s: error submitting urb(%d)", __func__, retval); |
| 345 | } else { |
| 346 | /* Wait for transmission to complete (or abort) */ |
| 347 | mutex_unlock(&context->ctx_lock); |
| 348 | retval = wait_for_completion_interruptible( |
| 349 | &context->tx.finished); |
| 350 | if (retval) |
| 351 | err("%s: task interrupted", __func__); |
| 352 | mutex_lock(&context->ctx_lock); |
| 353 | |
| 354 | retval = context->tx.status; |
| 355 | if (retval) |
| 356 | err("%s: packet tx failed (%d)", __func__, retval); |
| 357 | } |
| 358 | |
| 359 | kfree(control_req); |
| 360 | |
| 361 | return retval; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Writes data to the VFD. The iMON VFD is 2x16 characters |
| 366 | * and requires data in 5 consecutive USB interrupt packets, |
| 367 | * each packet but the last carrying 7 bytes. |
| 368 | * |
| 369 | * I don't know if the VFD board supports features such as |
| 370 | * scrolling, clearing rows, blanking, etc. so at |
| 371 | * the caller must provide a full screen of data. If fewer |
| 372 | * than 32 bytes are provided spaces will be appended to |
| 373 | * generate a full screen. |
| 374 | */ |
| 375 | static ssize_t vfd_write(struct file *file, const char *buf, |
| 376 | size_t n_bytes, loff_t *pos) |
| 377 | { |
| 378 | int i; |
| 379 | int offset; |
| 380 | int seq; |
| 381 | int retval = 0; |
| 382 | struct imon_context *context; |
| 383 | const unsigned char vfd_packet6[] = { |
| 384 | 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF }; |
| 385 | int *data_buf; |
| 386 | |
| 387 | context = (struct imon_context *)file->private_data; |
| 388 | if (!context) { |
| 389 | err("%s: no context for device", __func__); |
| 390 | return -ENODEV; |
| 391 | } |
| 392 | |
| 393 | mutex_lock(&context->ctx_lock); |
| 394 | |
| 395 | if (!context->dev_present) { |
| 396 | err("%s: no iMON device present", __func__); |
| 397 | retval = -ENODEV; |
| 398 | goto exit; |
| 399 | } |
| 400 | |
| 401 | if (n_bytes <= 0 || n_bytes > IMON_DATA_BUF_SZ - 3) { |
| 402 | err("%s: invalid payload size", __func__); |
| 403 | retval = -EINVAL; |
| 404 | goto exit; |
| 405 | } |
| 406 | |
| 407 | data_buf = memdup_user(buf, n_bytes); |
| 408 | if (IS_ERR(data_buf)) { |
| 409 | retval = PTR_ERR(data_buf); |
| 410 | goto exit; |
| 411 | } |
| 412 | |
| 413 | memcpy(context->tx.data_buf, data_buf, n_bytes); |
| 414 | |
| 415 | /* Pad with spaces */ |
| 416 | for (i = n_bytes; i < IMON_DATA_BUF_SZ - 3; ++i) |
| 417 | context->tx.data_buf[i] = ' '; |
| 418 | |
| 419 | for (i = IMON_DATA_BUF_SZ - 3; i < IMON_DATA_BUF_SZ; ++i) |
| 420 | context->tx.data_buf[i] = 0xFF; |
| 421 | |
| 422 | offset = 0; |
| 423 | seq = 0; |
| 424 | |
| 425 | do { |
| 426 | memcpy(context->usb_tx_buf, context->tx.data_buf + offset, 7); |
| 427 | context->usb_tx_buf[7] = (unsigned char) seq; |
| 428 | |
| 429 | retval = send_packet(context); |
| 430 | if (retval) { |
| 431 | err("%s: send packet failed for packet #%d", |
| 432 | __func__, seq/2); |
| 433 | goto exit; |
| 434 | } else { |
| 435 | seq += 2; |
| 436 | offset += 7; |
| 437 | } |
| 438 | |
| 439 | } while (offset < IMON_DATA_BUF_SZ); |
| 440 | |
| 441 | if (context->vfd_proto_6p) { |
| 442 | /* Send packet #6 */ |
| 443 | memcpy(context->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6)); |
| 444 | context->usb_tx_buf[7] = (unsigned char) seq; |
| 445 | retval = send_packet(context); |
| 446 | if (retval) |
| 447 | err("%s: send packet failed for packet #%d", |
| 448 | __func__, seq/2); |
| 449 | } |
| 450 | |
| 451 | exit: |
| 452 | mutex_unlock(&context->ctx_lock); |
| 453 | |
| 454 | return (!retval) ? n_bytes : retval; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Callback function for USB core API: transmit data |
| 459 | */ |
| 460 | static void usb_tx_callback(struct urb *urb) |
| 461 | { |
| 462 | struct imon_context *context; |
| 463 | |
| 464 | if (!urb) |
| 465 | return; |
| 466 | context = (struct imon_context *)urb->context; |
| 467 | if (!context) |
| 468 | return; |
| 469 | |
| 470 | context->tx.status = urb->status; |
| 471 | |
| 472 | /* notify waiters that write has finished */ |
| 473 | atomic_set(&context->tx.busy, 0); |
| 474 | complete(&context->tx.finished); |
| 475 | |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Called by lirc_dev when the application opens /dev/lirc |
| 481 | */ |
| 482 | static int ir_open(void *data) |
| 483 | { |
| 484 | int retval = 0; |
| 485 | struct imon_context *context; |
| 486 | |
| 487 | /* prevent races with disconnect */ |
| 488 | mutex_lock(&driver_lock); |
| 489 | |
| 490 | context = (struct imon_context *)data; |
| 491 | |
| 492 | /* initial IR protocol decode variables */ |
| 493 | context->rx.count = 0; |
| 494 | context->rx.initial_space = 1; |
| 495 | context->rx.prev_bit = 0; |
| 496 | |
| 497 | context->ir_isopen = 1; |
| 498 | dev_info(context->driver->dev, "IR port opened\n"); |
| 499 | |
| 500 | mutex_unlock(&driver_lock); |
| 501 | return retval; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Called by lirc_dev when the application closes /dev/lirc |
| 506 | */ |
| 507 | static void ir_close(void *data) |
| 508 | { |
| 509 | struct imon_context *context; |
| 510 | |
| 511 | context = (struct imon_context *)data; |
| 512 | if (!context) { |
| 513 | err("%s: no context for device", __func__); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | mutex_lock(&context->ctx_lock); |
| 518 | |
| 519 | context->ir_isopen = 0; |
| 520 | dev_info(context->driver->dev, "IR port closed\n"); |
| 521 | |
| 522 | if (!context->dev_present) { |
| 523 | /* |
| 524 | * Device disconnected while IR port was still open. Driver |
| 525 | * was not deregistered at disconnect time, so do it now. |
| 526 | */ |
| 527 | deregister_from_lirc(context); |
| 528 | |
| 529 | if (!context->display_isopen) { |
| 530 | mutex_unlock(&context->ctx_lock); |
| 531 | free_imon_context(context); |
| 532 | return; |
| 533 | } |
| 534 | /* |
| 535 | * If display port is open, context will be deleted by |
| 536 | * display_close |
| 537 | */ |
| 538 | } |
| 539 | |
| 540 | mutex_unlock(&context->ctx_lock); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Convert bit count to time duration (in us) and submit |
| 546 | * the value to lirc_dev. |
| 547 | */ |
| 548 | static void submit_data(struct imon_context *context) |
| 549 | { |
| 550 | unsigned char buf[4]; |
| 551 | int value = context->rx.count; |
| 552 | int i; |
| 553 | |
| 554 | dev_dbg(context->driver->dev, "submitting data to LIRC\n"); |
| 555 | |
| 556 | value *= BIT_DURATION; |
| 557 | value &= PULSE_MASK; |
| 558 | if (context->rx.prev_bit) |
| 559 | value |= PULSE_BIT; |
| 560 | |
| 561 | for (i = 0; i < 4; ++i) |
| 562 | buf[i] = value>>(i*8); |
| 563 | |
| 564 | lirc_buffer_write(context->driver->rbuf, buf); |
| 565 | wake_up(&context->driver->rbuf->wait_poll); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | static inline int tv2int(const struct timeval *a, const struct timeval *b) |
| 570 | { |
| 571 | int usecs = 0; |
| 572 | int sec = 0; |
| 573 | |
| 574 | if (b->tv_usec > a->tv_usec) { |
| 575 | usecs = 1000000; |
| 576 | sec--; |
| 577 | } |
| 578 | |
| 579 | usecs += a->tv_usec - b->tv_usec; |
| 580 | |
| 581 | sec += a->tv_sec - b->tv_sec; |
| 582 | sec *= 1000; |
| 583 | usecs /= 1000; |
| 584 | sec += usecs; |
| 585 | |
| 586 | if (sec < 0) |
| 587 | sec = 1000; |
| 588 | |
| 589 | return sec; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Process the incoming packet |
| 594 | */ |
| 595 | static void imon_incoming_packet(struct imon_context *context, |
| 596 | struct urb *urb, int intf) |
| 597 | { |
| 598 | int len = urb->actual_length; |
| 599 | unsigned char *buf = urb->transfer_buffer; |
| 600 | struct device *dev = context->driver->dev; |
| 601 | int octet, bit; |
| 602 | unsigned char mask; |
| 603 | int i, chunk_num; |
| 604 | |
| 605 | /* |
| 606 | * just bail out if no listening IR client |
| 607 | */ |
| 608 | if (!context->ir_isopen) |
| 609 | return; |
| 610 | |
| 611 | if (len != 8) { |
| 612 | dev_warn(dev, "imon %s: invalid incoming packet " |
| 613 | "size (len = %d, intf%d)\n", __func__, len, intf); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | if (debug) { |
| 618 | printk(KERN_INFO "raw packet: "); |
| 619 | for (i = 0; i < len; ++i) |
| 620 | printk("%02x ", buf[i]); |
| 621 | printk("\n"); |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Translate received data to pulse and space lengths. |
| 626 | * Received data is active low, i.e. pulses are 0 and |
| 627 | * spaces are 1. |
| 628 | * |
| 629 | * My original algorithm was essentially similar to |
| 630 | * Changwoo Ryu's with the exception that he switched |
| 631 | * the incoming bits to active high and also fed an |
| 632 | * initial space to LIRC at the start of a new sequence |
| 633 | * if the previous bit was a pulse. |
| 634 | * |
| 635 | * I've decided to adopt his algorithm. |
| 636 | */ |
| 637 | |
| 638 | if (buf[7] == 1 && context->rx.initial_space) { |
| 639 | /* LIRC requires a leading space */ |
| 640 | context->rx.prev_bit = 0; |
| 641 | context->rx.count = 4; |
| 642 | submit_data(context); |
| 643 | context->rx.count = 0; |
| 644 | } |
| 645 | |
| 646 | for (octet = 0; octet < 5; ++octet) { |
| 647 | mask = 0x80; |
| 648 | for (bit = 0; bit < 8; ++bit) { |
| 649 | int curr_bit = !(buf[octet] & mask); |
| 650 | if (curr_bit != context->rx.prev_bit) { |
| 651 | if (context->rx.count) { |
| 652 | submit_data(context); |
| 653 | context->rx.count = 0; |
| 654 | } |
| 655 | context->rx.prev_bit = curr_bit; |
| 656 | } |
| 657 | ++context->rx.count; |
| 658 | mask >>= 1; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (chunk_num == 10) { |
| 663 | if (context->rx.count) { |
| 664 | submit_data(context); |
| 665 | context->rx.count = 0; |
| 666 | } |
| 667 | context->rx.initial_space = context->rx.prev_bit; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Callback function for USB core API: receive data |
| 673 | */ |
| 674 | static void usb_rx_callback(struct urb *urb) |
| 675 | { |
| 676 | struct imon_context *context; |
| 677 | unsigned char *buf; |
| 678 | int len; |
| 679 | int intfnum = 0; |
| 680 | |
| 681 | if (!urb) |
| 682 | return; |
| 683 | |
| 684 | context = (struct imon_context *)urb->context; |
| 685 | if (!context) |
| 686 | return; |
| 687 | |
| 688 | buf = urb->transfer_buffer; |
| 689 | len = urb->actual_length; |
| 690 | |
| 691 | switch (urb->status) { |
| 692 | case -ENOENT: /* usbcore unlink successful! */ |
| 693 | return; |
| 694 | |
| 695 | case 0: |
| 696 | imon_incoming_packet(context, urb, intfnum); |
| 697 | break; |
| 698 | |
| 699 | default: |
| 700 | dev_warn(context->driver->dev, "imon %s: status(%d): ignored\n", |
| 701 | __func__, urb->status); |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | usb_submit_urb(context->rx_urb, GFP_ATOMIC); |
| 706 | |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Callback function for USB core API: Probe |
| 712 | */ |
| 713 | static int imon_probe(struct usb_interface *interface, |
| 714 | const struct usb_device_id *id) |
| 715 | { |
| 716 | struct usb_device *usbdev = NULL; |
| 717 | struct usb_host_interface *iface_desc = NULL; |
| 718 | struct usb_endpoint_descriptor *rx_endpoint = NULL; |
| 719 | struct usb_endpoint_descriptor *tx_endpoint = NULL; |
| 720 | struct urb *rx_urb = NULL; |
| 721 | struct urb *tx_urb = NULL; |
| 722 | struct lirc_driver *driver = NULL; |
| 723 | struct lirc_buffer *rbuf = NULL; |
| 724 | struct device *dev = &interface->dev; |
| 725 | int ifnum; |
| 726 | int lirc_minor = 0; |
| 727 | int num_endpts; |
| 728 | int retval = 0; |
| 729 | int display_ep_found = 0; |
| 730 | int ir_ep_found = 0; |
| 731 | int alloc_status = 0; |
| 732 | int vfd_proto_6p = 0; |
| 733 | int code_length; |
| 734 | struct imon_context *context = NULL; |
| 735 | int i; |
| 736 | u16 vendor, product; |
| 737 | |
| 738 | context = kzalloc(sizeof(struct imon_context), GFP_KERNEL); |
| 739 | if (!context) { |
| 740 | err("%s: kzalloc failed for context", __func__); |
| 741 | alloc_status = 1; |
| 742 | goto alloc_status_switch; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Try to auto-detect the type of display if the user hasn't set |
| 747 | * it by hand via the display_type modparam. Default is VFD. |
| 748 | */ |
| 749 | if (usb_match_id(interface, ir_only_list)) |
| 750 | context->display = 0; |
| 751 | else |
| 752 | context->display = 1; |
| 753 | |
| 754 | code_length = BUF_CHUNK_SIZE * 8; |
| 755 | |
| 756 | usbdev = usb_get_dev(interface_to_usbdev(interface)); |
| 757 | iface_desc = interface->cur_altsetting; |
| 758 | num_endpts = iface_desc->desc.bNumEndpoints; |
| 759 | ifnum = iface_desc->desc.bInterfaceNumber; |
| 760 | vendor = le16_to_cpu(usbdev->descriptor.idVendor); |
| 761 | product = le16_to_cpu(usbdev->descriptor.idProduct); |
| 762 | |
| 763 | dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n", |
| 764 | __func__, vendor, product, ifnum); |
| 765 | |
| 766 | /* prevent races probing devices w/multiple interfaces */ |
| 767 | mutex_lock(&driver_lock); |
| 768 | |
| 769 | /* |
| 770 | * Scan the endpoint list and set: |
| 771 | * first input endpoint = IR endpoint |
| 772 | * first output endpoint = display endpoint |
| 773 | */ |
| 774 | for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) { |
| 775 | struct usb_endpoint_descriptor *ep; |
| 776 | int ep_dir; |
| 777 | int ep_type; |
| 778 | ep = &iface_desc->endpoint[i].desc; |
| 779 | ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK; |
| 780 | ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
| 781 | |
| 782 | if (!ir_ep_found && |
| 783 | ep_dir == USB_DIR_IN && |
| 784 | ep_type == USB_ENDPOINT_XFER_INT) { |
| 785 | |
| 786 | rx_endpoint = ep; |
| 787 | ir_ep_found = 1; |
| 788 | dev_dbg(dev, "%s: found IR endpoint\n", __func__); |
| 789 | |
| 790 | } else if (!display_ep_found && ep_dir == USB_DIR_OUT && |
| 791 | ep_type == USB_ENDPOINT_XFER_INT) { |
| 792 | tx_endpoint = ep; |
| 793 | display_ep_found = 1; |
| 794 | dev_dbg(dev, "%s: found display endpoint\n", __func__); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Some iMON receivers have no display. Unfortunately, it seems |
| 800 | * that SoundGraph recycles device IDs between devices both with |
| 801 | * and without... :\ |
| 802 | */ |
| 803 | if (context->display == 0) { |
| 804 | display_ep_found = 0; |
| 805 | dev_dbg(dev, "%s: device has no display\n", __func__); |
| 806 | } |
| 807 | |
| 808 | /* Input endpoint is mandatory */ |
| 809 | if (!ir_ep_found) { |
| 810 | err("%s: no valid input (IR) endpoint found.", __func__); |
| 811 | retval = -ENODEV; |
| 812 | alloc_status = 2; |
| 813 | goto alloc_status_switch; |
| 814 | } |
| 815 | |
| 816 | /* Determine if display requires 6 packets */ |
| 817 | if (display_ep_found) { |
| 818 | if (usb_match_id(interface, vfd_proto_6p_list)) |
| 819 | vfd_proto_6p = 1; |
| 820 | |
| 821 | dev_dbg(dev, "%s: vfd_proto_6p: %d\n", |
| 822 | __func__, vfd_proto_6p); |
| 823 | } |
| 824 | |
| 825 | driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); |
| 826 | if (!driver) { |
| 827 | err("%s: kzalloc failed for lirc_driver", __func__); |
| 828 | alloc_status = 2; |
| 829 | goto alloc_status_switch; |
| 830 | } |
| 831 | rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 832 | if (!rbuf) { |
| 833 | err("%s: kmalloc failed for lirc_buffer", __func__); |
| 834 | alloc_status = 3; |
| 835 | goto alloc_status_switch; |
| 836 | } |
| 837 | if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) { |
| 838 | err("%s: lirc_buffer_init failed", __func__); |
| 839 | alloc_status = 4; |
| 840 | goto alloc_status_switch; |
| 841 | } |
| 842 | rx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 843 | if (!rx_urb) { |
| 844 | err("%s: usb_alloc_urb failed for IR urb", __func__); |
| 845 | alloc_status = 5; |
| 846 | goto alloc_status_switch; |
| 847 | } |
| 848 | tx_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 849 | if (!tx_urb) { |
| 850 | err("%s: usb_alloc_urb failed for display urb", |
| 851 | __func__); |
| 852 | alloc_status = 6; |
| 853 | goto alloc_status_switch; |
| 854 | } |
| 855 | |
| 856 | mutex_init(&context->ctx_lock); |
| 857 | context->vfd_proto_6p = vfd_proto_6p; |
| 858 | |
| 859 | strcpy(driver->name, MOD_NAME); |
| 860 | driver->minor = -1; |
| 861 | driver->code_length = sizeof(int) * 8; |
| 862 | driver->sample_rate = 0; |
| 863 | driver->features = LIRC_CAN_REC_MODE2; |
| 864 | driver->data = context; |
| 865 | driver->rbuf = rbuf; |
| 866 | driver->set_use_inc = ir_open; |
| 867 | driver->set_use_dec = ir_close; |
| 868 | driver->dev = &interface->dev; |
| 869 | driver->owner = THIS_MODULE; |
| 870 | |
| 871 | mutex_lock(&context->ctx_lock); |
| 872 | |
| 873 | context->driver = driver; |
| 874 | /* start out in keyboard mode */ |
| 875 | |
| 876 | lirc_minor = lirc_register_driver(driver); |
| 877 | if (lirc_minor < 0) { |
| 878 | err("%s: lirc_register_driver failed", __func__); |
| 879 | alloc_status = 7; |
| 880 | goto alloc_status_switch; |
| 881 | } else |
| 882 | dev_info(dev, "Registered iMON driver " |
| 883 | "(lirc minor: %d)\n", lirc_minor); |
| 884 | |
| 885 | /* Needed while unregistering! */ |
| 886 | driver->minor = lirc_minor; |
| 887 | |
| 888 | context->usbdev = usbdev; |
| 889 | context->dev_present = 1; |
| 890 | context->rx_endpoint = rx_endpoint; |
| 891 | context->rx_urb = rx_urb; |
| 892 | |
| 893 | /* |
| 894 | * tx is used to send characters to lcd/vfd, associate RF |
| 895 | * remotes, set IR protocol, and maybe more... |
| 896 | */ |
| 897 | context->tx_endpoint = tx_endpoint; |
| 898 | context->tx_urb = tx_urb; |
| 899 | |
| 900 | if (display_ep_found) |
| 901 | context->display = 1; |
| 902 | |
| 903 | usb_fill_int_urb(context->rx_urb, context->usbdev, |
| 904 | usb_rcvintpipe(context->usbdev, |
| 905 | context->rx_endpoint->bEndpointAddress), |
| 906 | context->usb_rx_buf, sizeof(context->usb_rx_buf), |
| 907 | usb_rx_callback, context, |
| 908 | context->rx_endpoint->bInterval); |
| 909 | |
| 910 | retval = usb_submit_urb(context->rx_urb, GFP_KERNEL); |
| 911 | |
| 912 | if (retval) { |
| 913 | err("%s: usb_submit_urb failed for intf0 (%d)", |
| 914 | __func__, retval); |
| 915 | mutex_unlock(&context->ctx_lock); |
| 916 | goto exit; |
| 917 | } |
| 918 | |
| 919 | usb_set_intfdata(interface, context); |
| 920 | |
| 921 | if (context->display && ifnum == 0) { |
| 922 | dev_dbg(dev, "%s: Registering iMON display with sysfs\n", |
| 923 | __func__); |
| 924 | |
| 925 | if (usb_register_dev(interface, &imon_class)) { |
| 926 | /* Not a fatal error, so ignore */ |
| 927 | dev_info(dev, "%s: could not get a minor number for " |
| 928 | "display\n", __func__); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | dev_info(dev, "iMON device (%04x:%04x, intf%d) on " |
| 933 | "usb<%d:%d> initialized\n", vendor, product, ifnum, |
| 934 | usbdev->bus->busnum, usbdev->devnum); |
| 935 | |
| 936 | alloc_status_switch: |
| 937 | mutex_unlock(&context->ctx_lock); |
| 938 | |
| 939 | switch (alloc_status) { |
| 940 | case 7: |
| 941 | usb_free_urb(tx_urb); |
| 942 | case 6: |
| 943 | usb_free_urb(rx_urb); |
| 944 | case 5: |
| 945 | if (rbuf) |
| 946 | lirc_buffer_free(rbuf); |
| 947 | case 4: |
| 948 | kfree(rbuf); |
| 949 | case 3: |
| 950 | kfree(driver); |
| 951 | case 2: |
| 952 | kfree(context); |
| 953 | context = NULL; |
| 954 | case 1: |
| 955 | if (retval != -ENODEV) |
| 956 | retval = -ENOMEM; |
| 957 | break; |
| 958 | case 0: |
| 959 | retval = 0; |
| 960 | } |
| 961 | |
| 962 | exit: |
| 963 | mutex_unlock(&driver_lock); |
| 964 | |
| 965 | return retval; |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * Callback function for USB core API: disconnect |
| 970 | */ |
| 971 | static void imon_disconnect(struct usb_interface *interface) |
| 972 | { |
| 973 | struct imon_context *context; |
| 974 | int ifnum; |
| 975 | |
| 976 | /* prevent races with ir_open()/display_open() */ |
| 977 | mutex_lock(&driver_lock); |
| 978 | |
| 979 | context = usb_get_intfdata(interface); |
| 980 | ifnum = interface->cur_altsetting->desc.bInterfaceNumber; |
| 981 | |
| 982 | mutex_lock(&context->ctx_lock); |
| 983 | |
| 984 | usb_set_intfdata(interface, NULL); |
| 985 | |
| 986 | /* Abort ongoing write */ |
| 987 | if (atomic_read(&context->tx.busy)) { |
| 988 | usb_kill_urb(context->tx_urb); |
| 989 | complete_all(&context->tx.finished); |
| 990 | } |
| 991 | |
| 992 | context->dev_present = 0; |
| 993 | usb_kill_urb(context->rx_urb); |
| 994 | if (context->display) |
| 995 | usb_deregister_dev(interface, &imon_class); |
| 996 | |
| 997 | if (!context->ir_isopen && !context->dev_present) { |
| 998 | deregister_from_lirc(context); |
| 999 | mutex_unlock(&context->ctx_lock); |
| 1000 | if (!context->display_isopen) |
| 1001 | free_imon_context(context); |
| 1002 | } else |
| 1003 | mutex_unlock(&context->ctx_lock); |
| 1004 | |
| 1005 | mutex_unlock(&driver_lock); |
| 1006 | |
| 1007 | printk(KERN_INFO "%s: iMON device (intf%d) disconnected\n", |
| 1008 | __func__, ifnum); |
| 1009 | } |
| 1010 | |
| 1011 | static int imon_suspend(struct usb_interface *intf, pm_message_t message) |
| 1012 | { |
| 1013 | struct imon_context *context = usb_get_intfdata(intf); |
| 1014 | |
| 1015 | usb_kill_urb(context->rx_urb); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static int imon_resume(struct usb_interface *intf) |
| 1021 | { |
| 1022 | int rc = 0; |
| 1023 | struct imon_context *context = usb_get_intfdata(intf); |
| 1024 | |
| 1025 | usb_fill_int_urb(context->rx_urb, context->usbdev, |
| 1026 | usb_rcvintpipe(context->usbdev, |
| 1027 | context->rx_endpoint->bEndpointAddress), |
| 1028 | context->usb_rx_buf, sizeof(context->usb_rx_buf), |
| 1029 | usb_rx_callback, context, |
| 1030 | context->rx_endpoint->bInterval); |
| 1031 | |
| 1032 | rc = usb_submit_urb(context->rx_urb, GFP_ATOMIC); |
| 1033 | |
| 1034 | return rc; |
| 1035 | } |
| 1036 | |
| 1037 | static int __init imon_init(void) |
| 1038 | { |
| 1039 | int rc; |
| 1040 | |
| 1041 | printk(KERN_INFO MOD_NAME ": " MOD_DESC ", v" MOD_VERSION "\n"); |
| 1042 | |
| 1043 | rc = usb_register(&imon_driver); |
| 1044 | if (rc) { |
| 1045 | err("%s: usb register failed(%d)", __func__, rc); |
| 1046 | return -ENODEV; |
| 1047 | } |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static void __exit imon_exit(void) |
| 1053 | { |
| 1054 | usb_deregister(&imon_driver); |
| 1055 | printk(KERN_INFO MOD_NAME ": module removed. Goodbye!\n"); |
| 1056 | } |
| 1057 | |
| 1058 | module_init(imon_init); |
| 1059 | module_exit(imon_exit); |