| .. | .. |
|---|
| 157 | 157 | #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD |
|---|
| 158 | 158 | |
|---|
| 159 | 159 | struct tower_reset_reply { |
|---|
| 160 | | - __le16 size; /* little-endian */ |
|---|
| 160 | + __le16 size; |
|---|
| 161 | 161 | __u8 err_code; |
|---|
| 162 | 162 | __u8 spare; |
|---|
| 163 | | -} __attribute__ ((packed)); |
|---|
| 163 | +}; |
|---|
| 164 | 164 | |
|---|
| 165 | 165 | struct tower_get_version_reply { |
|---|
| 166 | | - __le16 size; /* little-endian */ |
|---|
| 166 | + __le16 size; |
|---|
| 167 | 167 | __u8 err_code; |
|---|
| 168 | 168 | __u8 spare; |
|---|
| 169 | 169 | __u8 major; |
|---|
| 170 | 170 | __u8 minor; |
|---|
| 171 | | - __le16 build_no; /* little-endian */ |
|---|
| 172 | | -} __attribute__ ((packed)); |
|---|
| 171 | + __le16 build_no; |
|---|
| 172 | +}; |
|---|
| 173 | 173 | |
|---|
| 174 | 174 | |
|---|
| 175 | 175 | /* table of devices that work with this driver */ |
|---|
| .. | .. |
|---|
| 178 | 178 | { } /* Terminating entry */ |
|---|
| 179 | 179 | }; |
|---|
| 180 | 180 | |
|---|
| 181 | | -MODULE_DEVICE_TABLE (usb, tower_table); |
|---|
| 181 | +MODULE_DEVICE_TABLE(usb, tower_table); |
|---|
| 182 | 182 | |
|---|
| 183 | 183 | #define LEGO_USB_TOWER_MINOR_BASE 160 |
|---|
| 184 | 184 | |
|---|
| .. | .. |
|---|
| 186 | 186 | /* Structure to hold all of our device specific stuff */ |
|---|
| 187 | 187 | struct lego_usb_tower { |
|---|
| 188 | 188 | struct mutex lock; /* locks this structure */ |
|---|
| 189 | | - struct usb_device* udev; /* save off the usb device pointer */ |
|---|
| 189 | + struct usb_device *udev; /* save off the usb device pointer */ |
|---|
| 190 | 190 | unsigned char minor; /* the starting minor number for this device */ |
|---|
| 191 | 191 | |
|---|
| 192 | 192 | int open_count; /* number of times this port has been opened */ |
|---|
| 193 | 193 | unsigned long disconnected:1; |
|---|
| 194 | 194 | |
|---|
| 195 | | - char* read_buffer; |
|---|
| 195 | + char *read_buffer; |
|---|
| 196 | 196 | size_t read_buffer_length; /* this much came in */ |
|---|
| 197 | 197 | size_t read_packet_length; /* this much will be returned on read */ |
|---|
| 198 | 198 | spinlock_t read_buffer_lock; |
|---|
| .. | .. |
|---|
| 202 | 202 | wait_queue_head_t read_wait; |
|---|
| 203 | 203 | wait_queue_head_t write_wait; |
|---|
| 204 | 204 | |
|---|
| 205 | | - char* interrupt_in_buffer; |
|---|
| 206 | | - struct usb_endpoint_descriptor* interrupt_in_endpoint; |
|---|
| 207 | | - struct urb* interrupt_in_urb; |
|---|
| 205 | + char *interrupt_in_buffer; |
|---|
| 206 | + struct usb_endpoint_descriptor *interrupt_in_endpoint; |
|---|
| 207 | + struct urb *interrupt_in_urb; |
|---|
| 208 | 208 | int interrupt_in_interval; |
|---|
| 209 | | - int interrupt_in_running; |
|---|
| 210 | 209 | int interrupt_in_done; |
|---|
| 211 | 210 | |
|---|
| 212 | | - char* interrupt_out_buffer; |
|---|
| 213 | | - struct usb_endpoint_descriptor* interrupt_out_endpoint; |
|---|
| 214 | | - struct urb* interrupt_out_urb; |
|---|
| 211 | + char *interrupt_out_buffer; |
|---|
| 212 | + struct usb_endpoint_descriptor *interrupt_out_endpoint; |
|---|
| 213 | + struct urb *interrupt_out_urb; |
|---|
| 215 | 214 | int interrupt_out_interval; |
|---|
| 216 | 215 | int interrupt_out_busy; |
|---|
| 217 | 216 | |
|---|
| .. | .. |
|---|
| 219 | 218 | |
|---|
| 220 | 219 | |
|---|
| 221 | 220 | /* local function prototypes */ |
|---|
| 222 | | -static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos); |
|---|
| 223 | | -static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos); |
|---|
| 224 | | -static inline void tower_delete (struct lego_usb_tower *dev); |
|---|
| 225 | | -static int tower_open (struct inode *inode, struct file *file); |
|---|
| 226 | | -static int tower_release (struct inode *inode, struct file *file); |
|---|
| 227 | | -static __poll_t tower_poll (struct file *file, poll_table *wait); |
|---|
| 228 | | -static loff_t tower_llseek (struct file *file, loff_t off, int whence); |
|---|
| 221 | +static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos); |
|---|
| 222 | +static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos); |
|---|
| 223 | +static inline void tower_delete(struct lego_usb_tower *dev); |
|---|
| 224 | +static int tower_open(struct inode *inode, struct file *file); |
|---|
| 225 | +static int tower_release(struct inode *inode, struct file *file); |
|---|
| 226 | +static __poll_t tower_poll(struct file *file, poll_table *wait); |
|---|
| 227 | +static loff_t tower_llseek(struct file *file, loff_t off, int whence); |
|---|
| 229 | 228 | |
|---|
| 230 | | -static void tower_abort_transfers (struct lego_usb_tower *dev); |
|---|
| 231 | | -static void tower_check_for_read_packet (struct lego_usb_tower *dev); |
|---|
| 232 | | -static void tower_interrupt_in_callback (struct urb *urb); |
|---|
| 233 | | -static void tower_interrupt_out_callback (struct urb *urb); |
|---|
| 229 | +static void tower_check_for_read_packet(struct lego_usb_tower *dev); |
|---|
| 230 | +static void tower_interrupt_in_callback(struct urb *urb); |
|---|
| 231 | +static void tower_interrupt_out_callback(struct urb *urb); |
|---|
| 234 | 232 | |
|---|
| 235 | | -static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id); |
|---|
| 236 | | -static void tower_disconnect (struct usb_interface *interface); |
|---|
| 233 | +static int tower_probe(struct usb_interface *interface, const struct usb_device_id *id); |
|---|
| 234 | +static void tower_disconnect(struct usb_interface *interface); |
|---|
| 237 | 235 | |
|---|
| 238 | 236 | |
|---|
| 239 | 237 | /* file operations needed when we register this driver */ |
|---|
| .. | .. |
|---|
| 273 | 271 | }; |
|---|
| 274 | 272 | |
|---|
| 275 | 273 | |
|---|
| 276 | | -/** |
|---|
| 274 | +/* |
|---|
| 277 | 275 | * lego_usb_tower_debug_data |
|---|
| 278 | 276 | */ |
|---|
| 279 | 277 | static inline void lego_usb_tower_debug_data(struct device *dev, |
|---|
| .. | .. |
|---|
| 285 | 283 | } |
|---|
| 286 | 284 | |
|---|
| 287 | 285 | |
|---|
| 288 | | -/** |
|---|
| 286 | +/* |
|---|
| 289 | 287 | * tower_delete |
|---|
| 290 | 288 | */ |
|---|
| 291 | | -static inline void tower_delete (struct lego_usb_tower *dev) |
|---|
| 289 | +static inline void tower_delete(struct lego_usb_tower *dev) |
|---|
| 292 | 290 | { |
|---|
| 293 | 291 | /* free data structures */ |
|---|
| 294 | 292 | usb_free_urb(dev->interrupt_in_urb); |
|---|
| 295 | 293 | usb_free_urb(dev->interrupt_out_urb); |
|---|
| 296 | | - kfree (dev->read_buffer); |
|---|
| 297 | | - kfree (dev->interrupt_in_buffer); |
|---|
| 298 | | - kfree (dev->interrupt_out_buffer); |
|---|
| 294 | + kfree(dev->read_buffer); |
|---|
| 295 | + kfree(dev->interrupt_in_buffer); |
|---|
| 296 | + kfree(dev->interrupt_out_buffer); |
|---|
| 299 | 297 | usb_put_dev(dev->udev); |
|---|
| 300 | | - kfree (dev); |
|---|
| 298 | + kfree(dev); |
|---|
| 301 | 299 | } |
|---|
| 302 | 300 | |
|---|
| 303 | 301 | |
|---|
| 304 | | -/** |
|---|
| 302 | +/* |
|---|
| 305 | 303 | * tower_open |
|---|
| 306 | 304 | */ |
|---|
| 307 | | -static int tower_open (struct inode *inode, struct file *file) |
|---|
| 305 | +static int tower_open(struct inode *inode, struct file *file) |
|---|
| 308 | 306 | { |
|---|
| 309 | 307 | struct lego_usb_tower *dev = NULL; |
|---|
| 310 | 308 | int subminor; |
|---|
| 311 | 309 | int retval = 0; |
|---|
| 312 | 310 | struct usb_interface *interface; |
|---|
| 313 | | - struct tower_reset_reply *reset_reply; |
|---|
| 311 | + struct tower_reset_reply reset_reply; |
|---|
| 314 | 312 | int result; |
|---|
| 315 | | - |
|---|
| 316 | | - reset_reply = kmalloc(sizeof(*reset_reply), GFP_KERNEL); |
|---|
| 317 | | - |
|---|
| 318 | | - if (!reset_reply) { |
|---|
| 319 | | - retval = -ENOMEM; |
|---|
| 320 | | - goto exit; |
|---|
| 321 | | - } |
|---|
| 322 | 313 | |
|---|
| 323 | 314 | nonseekable_open(inode, file); |
|---|
| 324 | 315 | subminor = iminor(inode); |
|---|
| 325 | 316 | |
|---|
| 326 | | - interface = usb_find_interface (&tower_driver, subminor); |
|---|
| 327 | | - |
|---|
| 317 | + interface = usb_find_interface(&tower_driver, subminor); |
|---|
| 328 | 318 | if (!interface) { |
|---|
| 329 | 319 | pr_err("error, can't find device for minor %d\n", subminor); |
|---|
| 330 | 320 | retval = -ENODEV; |
|---|
| .. | .. |
|---|
| 351 | 341 | } |
|---|
| 352 | 342 | |
|---|
| 353 | 343 | /* reset the tower */ |
|---|
| 354 | | - result = usb_control_msg (dev->udev, |
|---|
| 355 | | - usb_rcvctrlpipe(dev->udev, 0), |
|---|
| 356 | | - LEGO_USB_TOWER_REQUEST_RESET, |
|---|
| 357 | | - USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, |
|---|
| 358 | | - 0, |
|---|
| 359 | | - 0, |
|---|
| 360 | | - reset_reply, |
|---|
| 361 | | - sizeof(*reset_reply), |
|---|
| 362 | | - 1000); |
|---|
| 344 | + result = usb_control_msg_recv(dev->udev, 0, |
|---|
| 345 | + LEGO_USB_TOWER_REQUEST_RESET, |
|---|
| 346 | + USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, |
|---|
| 347 | + 0, 0, |
|---|
| 348 | + &reset_reply, sizeof(reset_reply), 1000, |
|---|
| 349 | + GFP_KERNEL); |
|---|
| 363 | 350 | if (result < 0) { |
|---|
| 364 | 351 | dev_err(&dev->udev->dev, |
|---|
| 365 | 352 | "LEGO USB Tower reset control request failed\n"); |
|---|
| .. | .. |
|---|
| 370 | 357 | /* initialize in direction */ |
|---|
| 371 | 358 | dev->read_buffer_length = 0; |
|---|
| 372 | 359 | dev->read_packet_length = 0; |
|---|
| 373 | | - usb_fill_int_urb (dev->interrupt_in_urb, |
|---|
| 374 | | - dev->udev, |
|---|
| 375 | | - usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress), |
|---|
| 376 | | - dev->interrupt_in_buffer, |
|---|
| 377 | | - usb_endpoint_maxp(dev->interrupt_in_endpoint), |
|---|
| 378 | | - tower_interrupt_in_callback, |
|---|
| 379 | | - dev, |
|---|
| 380 | | - dev->interrupt_in_interval); |
|---|
| 360 | + usb_fill_int_urb(dev->interrupt_in_urb, |
|---|
| 361 | + dev->udev, |
|---|
| 362 | + usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress), |
|---|
| 363 | + dev->interrupt_in_buffer, |
|---|
| 364 | + usb_endpoint_maxp(dev->interrupt_in_endpoint), |
|---|
| 365 | + tower_interrupt_in_callback, |
|---|
| 366 | + dev, |
|---|
| 367 | + dev->interrupt_in_interval); |
|---|
| 381 | 368 | |
|---|
| 382 | | - dev->interrupt_in_running = 1; |
|---|
| 383 | 369 | dev->interrupt_in_done = 0; |
|---|
| 384 | 370 | mb(); |
|---|
| 385 | 371 | |
|---|
| 386 | | - retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL); |
|---|
| 372 | + retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); |
|---|
| 387 | 373 | if (retval) { |
|---|
| 388 | 374 | dev_err(&dev->udev->dev, |
|---|
| 389 | 375 | "Couldn't submit interrupt_in_urb %d\n", retval); |
|---|
| 390 | | - dev->interrupt_in_running = 0; |
|---|
| 391 | 376 | goto unlock_exit; |
|---|
| 392 | 377 | } |
|---|
| 393 | 378 | |
|---|
| .. | .. |
|---|
| 400 | 385 | mutex_unlock(&dev->lock); |
|---|
| 401 | 386 | |
|---|
| 402 | 387 | exit: |
|---|
| 403 | | - kfree(reset_reply); |
|---|
| 404 | 388 | return retval; |
|---|
| 405 | 389 | } |
|---|
| 406 | 390 | |
|---|
| 407 | | -/** |
|---|
| 391 | +/* |
|---|
| 408 | 392 | * tower_release |
|---|
| 409 | 393 | */ |
|---|
| 410 | | -static int tower_release (struct inode *inode, struct file *file) |
|---|
| 394 | +static int tower_release(struct inode *inode, struct file *file) |
|---|
| 411 | 395 | { |
|---|
| 412 | 396 | struct lego_usb_tower *dev; |
|---|
| 413 | 397 | int retval = 0; |
|---|
| 414 | 398 | |
|---|
| 415 | 399 | dev = file->private_data; |
|---|
| 416 | | - |
|---|
| 417 | 400 | if (dev == NULL) { |
|---|
| 418 | 401 | retval = -ENODEV; |
|---|
| 419 | 402 | goto exit; |
|---|
| .. | .. |
|---|
| 421 | 404 | |
|---|
| 422 | 405 | mutex_lock(&dev->lock); |
|---|
| 423 | 406 | |
|---|
| 424 | | - if (dev->open_count != 1) { |
|---|
| 425 | | - dev_dbg(&dev->udev->dev, "%s: device not opened exactly once\n", |
|---|
| 426 | | - __func__); |
|---|
| 427 | | - retval = -ENODEV; |
|---|
| 428 | | - goto unlock_exit; |
|---|
| 429 | | - } |
|---|
| 430 | | - |
|---|
| 431 | 407 | if (dev->disconnected) { |
|---|
| 432 | 408 | /* the device was unplugged before the file was released */ |
|---|
| 433 | 409 | |
|---|
| 434 | 410 | /* unlock here as tower_delete frees dev */ |
|---|
| 435 | 411 | mutex_unlock(&dev->lock); |
|---|
| 436 | | - tower_delete (dev); |
|---|
| 412 | + tower_delete(dev); |
|---|
| 437 | 413 | goto exit; |
|---|
| 438 | 414 | } |
|---|
| 439 | 415 | |
|---|
| 440 | 416 | /* wait until write transfer is finished */ |
|---|
| 441 | 417 | if (dev->interrupt_out_busy) { |
|---|
| 442 | | - wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); |
|---|
| 418 | + wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, |
|---|
| 419 | + 2 * HZ); |
|---|
| 443 | 420 | } |
|---|
| 444 | | - tower_abort_transfers (dev); |
|---|
| 421 | + |
|---|
| 422 | + /* shutdown transfers */ |
|---|
| 423 | + usb_kill_urb(dev->interrupt_in_urb); |
|---|
| 424 | + usb_kill_urb(dev->interrupt_out_urb); |
|---|
| 425 | + |
|---|
| 445 | 426 | dev->open_count = 0; |
|---|
| 446 | 427 | |
|---|
| 447 | | -unlock_exit: |
|---|
| 448 | 428 | mutex_unlock(&dev->lock); |
|---|
| 449 | 429 | exit: |
|---|
| 450 | 430 | return retval; |
|---|
| 451 | 431 | } |
|---|
| 452 | 432 | |
|---|
| 453 | | - |
|---|
| 454 | | -/** |
|---|
| 455 | | - * tower_abort_transfers |
|---|
| 456 | | - * aborts transfers and frees associated data structures |
|---|
| 457 | | - */ |
|---|
| 458 | | -static void tower_abort_transfers (struct lego_usb_tower *dev) |
|---|
| 459 | | -{ |
|---|
| 460 | | - if (dev == NULL) |
|---|
| 461 | | - return; |
|---|
| 462 | | - |
|---|
| 463 | | - /* shutdown transfer */ |
|---|
| 464 | | - if (dev->interrupt_in_running) { |
|---|
| 465 | | - dev->interrupt_in_running = 0; |
|---|
| 466 | | - mb(); |
|---|
| 467 | | - usb_kill_urb(dev->interrupt_in_urb); |
|---|
| 468 | | - } |
|---|
| 469 | | - if (dev->interrupt_out_busy) |
|---|
| 470 | | - usb_kill_urb(dev->interrupt_out_urb); |
|---|
| 471 | | -} |
|---|
| 472 | | - |
|---|
| 473 | | - |
|---|
| 474 | | -/** |
|---|
| 433 | +/* |
|---|
| 475 | 434 | * tower_check_for_read_packet |
|---|
| 476 | 435 | * |
|---|
| 477 | 436 | * To get correct semantics for signals and non-blocking I/O |
|---|
| .. | .. |
|---|
| 479 | 438 | * until it has been there unchanged for at least |
|---|
| 480 | 439 | * dev->packet_timeout_jiffies, or until the buffer is full. |
|---|
| 481 | 440 | */ |
|---|
| 482 | | -static void tower_check_for_read_packet (struct lego_usb_tower *dev) |
|---|
| 441 | +static void tower_check_for_read_packet(struct lego_usb_tower *dev) |
|---|
| 483 | 442 | { |
|---|
| 484 | | - spin_lock_irq (&dev->read_buffer_lock); |
|---|
| 443 | + spin_lock_irq(&dev->read_buffer_lock); |
|---|
| 485 | 444 | if (!packet_timeout |
|---|
| 486 | 445 | || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies) |
|---|
| 487 | 446 | || dev->read_buffer_length == read_buffer_size) { |
|---|
| 488 | 447 | dev->read_packet_length = dev->read_buffer_length; |
|---|
| 489 | 448 | } |
|---|
| 490 | 449 | dev->interrupt_in_done = 0; |
|---|
| 491 | | - spin_unlock_irq (&dev->read_buffer_lock); |
|---|
| 450 | + spin_unlock_irq(&dev->read_buffer_lock); |
|---|
| 492 | 451 | } |
|---|
| 493 | 452 | |
|---|
| 494 | 453 | |
|---|
| 495 | | -/** |
|---|
| 454 | +/* |
|---|
| 496 | 455 | * tower_poll |
|---|
| 497 | 456 | */ |
|---|
| 498 | | -static __poll_t tower_poll (struct file *file, poll_table *wait) |
|---|
| 457 | +static __poll_t tower_poll(struct file *file, poll_table *wait) |
|---|
| 499 | 458 | { |
|---|
| 500 | 459 | struct lego_usb_tower *dev; |
|---|
| 501 | 460 | __poll_t mask = 0; |
|---|
| .. | .. |
|---|
| 509 | 468 | poll_wait(file, &dev->write_wait, wait); |
|---|
| 510 | 469 | |
|---|
| 511 | 470 | tower_check_for_read_packet(dev); |
|---|
| 512 | | - if (dev->read_packet_length > 0) { |
|---|
| 471 | + if (dev->read_packet_length > 0) |
|---|
| 513 | 472 | mask |= EPOLLIN | EPOLLRDNORM; |
|---|
| 514 | | - } |
|---|
| 515 | | - if (!dev->interrupt_out_busy) { |
|---|
| 473 | + if (!dev->interrupt_out_busy) |
|---|
| 516 | 474 | mask |= EPOLLOUT | EPOLLWRNORM; |
|---|
| 517 | | - } |
|---|
| 518 | 475 | |
|---|
| 519 | 476 | return mask; |
|---|
| 520 | 477 | } |
|---|
| 521 | 478 | |
|---|
| 522 | 479 | |
|---|
| 523 | | -/** |
|---|
| 480 | +/* |
|---|
| 524 | 481 | * tower_llseek |
|---|
| 525 | 482 | */ |
|---|
| 526 | | -static loff_t tower_llseek (struct file *file, loff_t off, int whence) |
|---|
| 483 | +static loff_t tower_llseek(struct file *file, loff_t off, int whence) |
|---|
| 527 | 484 | { |
|---|
| 528 | 485 | return -ESPIPE; /* unseekable */ |
|---|
| 529 | 486 | } |
|---|
| 530 | 487 | |
|---|
| 531 | 488 | |
|---|
| 532 | | -/** |
|---|
| 489 | +/* |
|---|
| 533 | 490 | * tower_read |
|---|
| 534 | 491 | */ |
|---|
| 535 | | -static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
|---|
| 492 | +static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
|---|
| 536 | 493 | { |
|---|
| 537 | 494 | struct lego_usb_tower *dev; |
|---|
| 538 | 495 | size_t bytes_to_read; |
|---|
| .. | .. |
|---|
| 551 | 508 | /* verify that the device wasn't unplugged */ |
|---|
| 552 | 509 | if (dev->disconnected) { |
|---|
| 553 | 510 | retval = -ENODEV; |
|---|
| 554 | | - pr_err("No device or device unplugged %d\n", retval); |
|---|
| 555 | 511 | goto unlock_exit; |
|---|
| 556 | 512 | } |
|---|
| 557 | 513 | |
|---|
| .. | .. |
|---|
| 561 | 517 | goto unlock_exit; |
|---|
| 562 | 518 | } |
|---|
| 563 | 519 | |
|---|
| 564 | | - if (read_timeout) { |
|---|
| 520 | + if (read_timeout) |
|---|
| 565 | 521 | timeout = jiffies + msecs_to_jiffies(read_timeout); |
|---|
| 566 | | - } |
|---|
| 567 | 522 | |
|---|
| 568 | 523 | /* wait for data */ |
|---|
| 569 | | - tower_check_for_read_packet (dev); |
|---|
| 524 | + tower_check_for_read_packet(dev); |
|---|
| 570 | 525 | while (dev->read_packet_length == 0) { |
|---|
| 571 | 526 | if (file->f_flags & O_NONBLOCK) { |
|---|
| 572 | 527 | retval = -EAGAIN; |
|---|
| 573 | 528 | goto unlock_exit; |
|---|
| 574 | 529 | } |
|---|
| 575 | 530 | retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies); |
|---|
| 576 | | - if (retval < 0) { |
|---|
| 531 | + if (retval < 0) |
|---|
| 577 | 532 | goto unlock_exit; |
|---|
| 578 | | - } |
|---|
| 579 | 533 | |
|---|
| 580 | 534 | /* reset read timeout during read or write activity */ |
|---|
| 581 | 535 | if (read_timeout |
|---|
| .. | .. |
|---|
| 583 | 537 | timeout = jiffies + msecs_to_jiffies(read_timeout); |
|---|
| 584 | 538 | } |
|---|
| 585 | 539 | /* check for read timeout */ |
|---|
| 586 | | - if (read_timeout && time_after (jiffies, timeout)) { |
|---|
| 540 | + if (read_timeout && time_after(jiffies, timeout)) { |
|---|
| 587 | 541 | retval = -ETIMEDOUT; |
|---|
| 588 | 542 | goto unlock_exit; |
|---|
| 589 | 543 | } |
|---|
| 590 | | - tower_check_for_read_packet (dev); |
|---|
| 544 | + tower_check_for_read_packet(dev); |
|---|
| 591 | 545 | } |
|---|
| 592 | 546 | |
|---|
| 593 | 547 | /* copy the data from read_buffer into userspace */ |
|---|
| 594 | 548 | bytes_to_read = min(count, dev->read_packet_length); |
|---|
| 595 | 549 | |
|---|
| 596 | | - if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) { |
|---|
| 550 | + if (copy_to_user(buffer, dev->read_buffer, bytes_to_read)) { |
|---|
| 597 | 551 | retval = -EFAULT; |
|---|
| 598 | 552 | goto unlock_exit; |
|---|
| 599 | 553 | } |
|---|
| 600 | 554 | |
|---|
| 601 | | - spin_lock_irq (&dev->read_buffer_lock); |
|---|
| 555 | + spin_lock_irq(&dev->read_buffer_lock); |
|---|
| 602 | 556 | dev->read_buffer_length -= bytes_to_read; |
|---|
| 603 | 557 | dev->read_packet_length -= bytes_to_read; |
|---|
| 604 | | - for (i=0; i<dev->read_buffer_length; i++) { |
|---|
| 558 | + for (i = 0; i < dev->read_buffer_length; i++) |
|---|
| 605 | 559 | dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read]; |
|---|
| 606 | | - } |
|---|
| 607 | | - spin_unlock_irq (&dev->read_buffer_lock); |
|---|
| 560 | + spin_unlock_irq(&dev->read_buffer_lock); |
|---|
| 608 | 561 | |
|---|
| 609 | 562 | retval = bytes_to_read; |
|---|
| 610 | 563 | |
|---|
| .. | .. |
|---|
| 617 | 570 | } |
|---|
| 618 | 571 | |
|---|
| 619 | 572 | |
|---|
| 620 | | -/** |
|---|
| 573 | +/* |
|---|
| 621 | 574 | * tower_write |
|---|
| 622 | 575 | */ |
|---|
| 623 | | -static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
|---|
| 576 | +static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
|---|
| 624 | 577 | { |
|---|
| 625 | 578 | struct lego_usb_tower *dev; |
|---|
| 626 | 579 | size_t bytes_to_write; |
|---|
| .. | .. |
|---|
| 637 | 590 | /* verify that the device wasn't unplugged */ |
|---|
| 638 | 591 | if (dev->disconnected) { |
|---|
| 639 | 592 | retval = -ENODEV; |
|---|
| 640 | | - pr_err("No device or device unplugged %d\n", retval); |
|---|
| 641 | 593 | goto unlock_exit; |
|---|
| 642 | 594 | } |
|---|
| 643 | 595 | |
|---|
| .. | .. |
|---|
| 653 | 605 | retval = -EAGAIN; |
|---|
| 654 | 606 | goto unlock_exit; |
|---|
| 655 | 607 | } |
|---|
| 656 | | - retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy); |
|---|
| 657 | | - if (retval) { |
|---|
| 608 | + retval = wait_event_interruptible(dev->write_wait, |
|---|
| 609 | + !dev->interrupt_out_busy); |
|---|
| 610 | + if (retval) |
|---|
| 658 | 611 | goto unlock_exit; |
|---|
| 659 | | - } |
|---|
| 660 | 612 | } |
|---|
| 661 | 613 | |
|---|
| 662 | 614 | /* write the data into interrupt_out_buffer from userspace */ |
|---|
| .. | .. |
|---|
| 664 | 616 | dev_dbg(&dev->udev->dev, "%s: count = %zd, bytes_to_write = %zd\n", |
|---|
| 665 | 617 | __func__, count, bytes_to_write); |
|---|
| 666 | 618 | |
|---|
| 667 | | - if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) { |
|---|
| 619 | + if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { |
|---|
| 668 | 620 | retval = -EFAULT; |
|---|
| 669 | 621 | goto unlock_exit; |
|---|
| 670 | 622 | } |
|---|
| .. | .. |
|---|
| 682 | 634 | dev->interrupt_out_busy = 1; |
|---|
| 683 | 635 | wmb(); |
|---|
| 684 | 636 | |
|---|
| 685 | | - retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL); |
|---|
| 637 | + retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); |
|---|
| 686 | 638 | if (retval) { |
|---|
| 687 | 639 | dev->interrupt_out_busy = 0; |
|---|
| 688 | 640 | dev_err(&dev->udev->dev, |
|---|
| .. | .. |
|---|
| 700 | 652 | } |
|---|
| 701 | 653 | |
|---|
| 702 | 654 | |
|---|
| 703 | | -/** |
|---|
| 655 | +/* |
|---|
| 704 | 656 | * tower_interrupt_in_callback |
|---|
| 705 | 657 | */ |
|---|
| 706 | | -static void tower_interrupt_in_callback (struct urb *urb) |
|---|
| 658 | +static void tower_interrupt_in_callback(struct urb *urb) |
|---|
| 707 | 659 | { |
|---|
| 708 | 660 | struct lego_usb_tower *dev = urb->context; |
|---|
| 709 | 661 | int status = urb->status; |
|---|
| .. | .. |
|---|
| 729 | 681 | if (urb->actual_length > 0) { |
|---|
| 730 | 682 | spin_lock_irqsave(&dev->read_buffer_lock, flags); |
|---|
| 731 | 683 | if (dev->read_buffer_length + urb->actual_length < read_buffer_size) { |
|---|
| 732 | | - memcpy (dev->read_buffer + dev->read_buffer_length, |
|---|
| 733 | | - dev->interrupt_in_buffer, |
|---|
| 734 | | - urb->actual_length); |
|---|
| 684 | + memcpy(dev->read_buffer + dev->read_buffer_length, |
|---|
| 685 | + dev->interrupt_in_buffer, |
|---|
| 686 | + urb->actual_length); |
|---|
| 735 | 687 | dev->read_buffer_length += urb->actual_length; |
|---|
| 736 | 688 | dev->read_last_arrival = jiffies; |
|---|
| 737 | 689 | dev_dbg(&dev->udev->dev, "%s: received %d bytes\n", |
|---|
| .. | .. |
|---|
| 744 | 696 | } |
|---|
| 745 | 697 | |
|---|
| 746 | 698 | resubmit: |
|---|
| 747 | | - /* resubmit if we're still running */ |
|---|
| 748 | | - if (dev->interrupt_in_running) { |
|---|
| 749 | | - retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC); |
|---|
| 750 | | - if (retval) |
|---|
| 751 | | - dev_err(&dev->udev->dev, |
|---|
| 752 | | - "%s: usb_submit_urb failed (%d)\n", |
|---|
| 753 | | - __func__, retval); |
|---|
| 699 | + retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); |
|---|
| 700 | + if (retval) { |
|---|
| 701 | + dev_err(&dev->udev->dev, "%s: usb_submit_urb failed (%d)\n", |
|---|
| 702 | + __func__, retval); |
|---|
| 754 | 703 | } |
|---|
| 755 | | - |
|---|
| 756 | 704 | exit: |
|---|
| 757 | 705 | dev->interrupt_in_done = 1; |
|---|
| 758 | | - wake_up_interruptible (&dev->read_wait); |
|---|
| 706 | + wake_up_interruptible(&dev->read_wait); |
|---|
| 759 | 707 | } |
|---|
| 760 | 708 | |
|---|
| 761 | 709 | |
|---|
| 762 | | -/** |
|---|
| 710 | +/* |
|---|
| 763 | 711 | * tower_interrupt_out_callback |
|---|
| 764 | 712 | */ |
|---|
| 765 | | -static void tower_interrupt_out_callback (struct urb *urb) |
|---|
| 713 | +static void tower_interrupt_out_callback(struct urb *urb) |
|---|
| 766 | 714 | { |
|---|
| 767 | 715 | struct lego_usb_tower *dev = urb->context; |
|---|
| 768 | 716 | int status = urb->status; |
|---|
| .. | .. |
|---|
| 784 | 732 | } |
|---|
| 785 | 733 | |
|---|
| 786 | 734 | |
|---|
| 787 | | -/** |
|---|
| 735 | +/* |
|---|
| 788 | 736 | * tower_probe |
|---|
| 789 | 737 | * |
|---|
| 790 | 738 | * Called by the usb core when a new device is connected that it thinks |
|---|
| 791 | 739 | * this driver might be interested in. |
|---|
| 792 | 740 | */ |
|---|
| 793 | | -static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id) |
|---|
| 741 | +static int tower_probe(struct usb_interface *interface, const struct usb_device_id *id) |
|---|
| 794 | 742 | { |
|---|
| 795 | 743 | struct device *idev = &interface->dev; |
|---|
| 796 | 744 | struct usb_device *udev = interface_to_usbdev(interface); |
|---|
| 797 | | - struct lego_usb_tower *dev = NULL; |
|---|
| 798 | | - struct tower_get_version_reply *get_version_reply = NULL; |
|---|
| 745 | + struct lego_usb_tower *dev; |
|---|
| 746 | + struct tower_get_version_reply get_version_reply; |
|---|
| 799 | 747 | int retval = -ENOMEM; |
|---|
| 800 | 748 | int result; |
|---|
| 801 | 749 | |
|---|
| 802 | 750 | /* allocate memory for our device state and initialize it */ |
|---|
| 803 | | - |
|---|
| 804 | | - dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL); |
|---|
| 805 | | - |
|---|
| 751 | + dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
|---|
| 806 | 752 | if (!dev) |
|---|
| 807 | 753 | goto exit; |
|---|
| 808 | 754 | |
|---|
| 809 | 755 | mutex_init(&dev->lock); |
|---|
| 810 | | - |
|---|
| 811 | 756 | dev->udev = usb_get_dev(udev); |
|---|
| 812 | | - dev->open_count = 0; |
|---|
| 813 | | - dev->disconnected = 0; |
|---|
| 814 | | - |
|---|
| 815 | | - dev->read_buffer = NULL; |
|---|
| 816 | | - dev->read_buffer_length = 0; |
|---|
| 817 | | - dev->read_packet_length = 0; |
|---|
| 818 | | - spin_lock_init (&dev->read_buffer_lock); |
|---|
| 757 | + spin_lock_init(&dev->read_buffer_lock); |
|---|
| 819 | 758 | dev->packet_timeout_jiffies = msecs_to_jiffies(packet_timeout); |
|---|
| 820 | 759 | dev->read_last_arrival = jiffies; |
|---|
| 821 | | - |
|---|
| 822 | | - init_waitqueue_head (&dev->read_wait); |
|---|
| 823 | | - init_waitqueue_head (&dev->write_wait); |
|---|
| 824 | | - |
|---|
| 825 | | - dev->interrupt_in_buffer = NULL; |
|---|
| 826 | | - dev->interrupt_in_endpoint = NULL; |
|---|
| 827 | | - dev->interrupt_in_urb = NULL; |
|---|
| 828 | | - dev->interrupt_in_running = 0; |
|---|
| 829 | | - dev->interrupt_in_done = 0; |
|---|
| 830 | | - |
|---|
| 831 | | - dev->interrupt_out_buffer = NULL; |
|---|
| 832 | | - dev->interrupt_out_endpoint = NULL; |
|---|
| 833 | | - dev->interrupt_out_urb = NULL; |
|---|
| 834 | | - dev->interrupt_out_busy = 0; |
|---|
| 760 | + init_waitqueue_head(&dev->read_wait); |
|---|
| 761 | + init_waitqueue_head(&dev->write_wait); |
|---|
| 835 | 762 | |
|---|
| 836 | 763 | result = usb_find_common_endpoints_reverse(interface->cur_altsetting, |
|---|
| 837 | 764 | NULL, NULL, |
|---|
| .. | .. |
|---|
| 843 | 770 | goto error; |
|---|
| 844 | 771 | } |
|---|
| 845 | 772 | |
|---|
| 846 | | - dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL); |
|---|
| 773 | + dev->read_buffer = kmalloc(read_buffer_size, GFP_KERNEL); |
|---|
| 847 | 774 | if (!dev->read_buffer) |
|---|
| 848 | 775 | goto error; |
|---|
| 849 | | - dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL); |
|---|
| 776 | + dev->interrupt_in_buffer = kmalloc(usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL); |
|---|
| 850 | 777 | if (!dev->interrupt_in_buffer) |
|---|
| 851 | 778 | goto error; |
|---|
| 852 | 779 | dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
|---|
| 853 | 780 | if (!dev->interrupt_in_urb) |
|---|
| 854 | 781 | goto error; |
|---|
| 855 | | - dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL); |
|---|
| 782 | + dev->interrupt_out_buffer = kmalloc(write_buffer_size, GFP_KERNEL); |
|---|
| 856 | 783 | if (!dev->interrupt_out_buffer) |
|---|
| 857 | 784 | goto error; |
|---|
| 858 | 785 | dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 861 | 788 | dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; |
|---|
| 862 | 789 | dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; |
|---|
| 863 | 790 | |
|---|
| 864 | | - get_version_reply = kmalloc(sizeof(*get_version_reply), GFP_KERNEL); |
|---|
| 865 | | - |
|---|
| 866 | | - if (!get_version_reply) { |
|---|
| 867 | | - retval = -ENOMEM; |
|---|
| 868 | | - goto error; |
|---|
| 869 | | - } |
|---|
| 870 | | - |
|---|
| 871 | 791 | /* get the firmware version and log it */ |
|---|
| 872 | | - result = usb_control_msg (udev, |
|---|
| 873 | | - usb_rcvctrlpipe(udev, 0), |
|---|
| 874 | | - LEGO_USB_TOWER_REQUEST_GET_VERSION, |
|---|
| 875 | | - USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, |
|---|
| 876 | | - 0, |
|---|
| 877 | | - 0, |
|---|
| 878 | | - get_version_reply, |
|---|
| 879 | | - sizeof(*get_version_reply), |
|---|
| 880 | | - 1000); |
|---|
| 881 | | - if (result != sizeof(*get_version_reply)) { |
|---|
| 882 | | - if (result >= 0) |
|---|
| 883 | | - result = -EIO; |
|---|
| 792 | + result = usb_control_msg_recv(udev, 0, |
|---|
| 793 | + LEGO_USB_TOWER_REQUEST_GET_VERSION, |
|---|
| 794 | + USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE, |
|---|
| 795 | + 0, |
|---|
| 796 | + 0, |
|---|
| 797 | + &get_version_reply, |
|---|
| 798 | + sizeof(get_version_reply), |
|---|
| 799 | + 1000, GFP_KERNEL); |
|---|
| 800 | + if (result) { |
|---|
| 884 | 801 | dev_err(idev, "get version request failed: %d\n", result); |
|---|
| 885 | 802 | retval = result; |
|---|
| 886 | 803 | goto error; |
|---|
| 887 | 804 | } |
|---|
| 888 | 805 | dev_info(&interface->dev, |
|---|
| 889 | 806 | "LEGO USB Tower firmware version is %d.%d build %d\n", |
|---|
| 890 | | - get_version_reply->major, |
|---|
| 891 | | - get_version_reply->minor, |
|---|
| 892 | | - le16_to_cpu(get_version_reply->build_no)); |
|---|
| 807 | + get_version_reply.major, |
|---|
| 808 | + get_version_reply.minor, |
|---|
| 809 | + le16_to_cpu(get_version_reply.build_no)); |
|---|
| 893 | 810 | |
|---|
| 894 | 811 | /* we can register the device now, as it is ready */ |
|---|
| 895 | | - usb_set_intfdata (interface, dev); |
|---|
| 812 | + usb_set_intfdata(interface, dev); |
|---|
| 896 | 813 | |
|---|
| 897 | | - retval = usb_register_dev (interface, &tower_class); |
|---|
| 898 | | - |
|---|
| 814 | + retval = usb_register_dev(interface, &tower_class); |
|---|
| 899 | 815 | if (retval) { |
|---|
| 900 | 816 | /* something prevented us from registering this driver */ |
|---|
| 901 | 817 | dev_err(idev, "Not able to get a minor for this device.\n"); |
|---|
| .. | .. |
|---|
| 909 | 825 | USB_MAJOR, dev->minor); |
|---|
| 910 | 826 | |
|---|
| 911 | 827 | exit: |
|---|
| 912 | | - kfree(get_version_reply); |
|---|
| 913 | 828 | return retval; |
|---|
| 914 | 829 | |
|---|
| 915 | 830 | error: |
|---|
| 916 | | - kfree(get_version_reply); |
|---|
| 917 | 831 | tower_delete(dev); |
|---|
| 918 | 832 | return retval; |
|---|
| 919 | 833 | } |
|---|
| 920 | 834 | |
|---|
| 921 | 835 | |
|---|
| 922 | | -/** |
|---|
| 836 | +/* |
|---|
| 923 | 837 | * tower_disconnect |
|---|
| 924 | 838 | * |
|---|
| 925 | 839 | * Called by the usb core when the device is removed from the system. |
|---|
| 926 | 840 | */ |
|---|
| 927 | | -static void tower_disconnect (struct usb_interface *interface) |
|---|
| 841 | +static void tower_disconnect(struct usb_interface *interface) |
|---|
| 928 | 842 | { |
|---|
| 929 | 843 | struct lego_usb_tower *dev; |
|---|
| 930 | 844 | int minor; |
|---|
| 931 | 845 | |
|---|
| 932 | | - dev = usb_get_intfdata (interface); |
|---|
| 846 | + dev = usb_get_intfdata(interface); |
|---|
| 933 | 847 | |
|---|
| 934 | 848 | minor = dev->minor; |
|---|
| 935 | 849 | |
|---|
| 936 | 850 | /* give back our minor and prevent further open() */ |
|---|
| 937 | | - usb_deregister_dev (interface, &tower_class); |
|---|
| 851 | + usb_deregister_dev(interface, &tower_class); |
|---|
| 938 | 852 | |
|---|
| 939 | 853 | /* stop I/O */ |
|---|
| 940 | 854 | usb_poison_urb(dev->interrupt_in_urb); |
|---|
| .. | .. |
|---|
| 945 | 859 | /* if the device is not opened, then we clean up right now */ |
|---|
| 946 | 860 | if (!dev->open_count) { |
|---|
| 947 | 861 | mutex_unlock(&dev->lock); |
|---|
| 948 | | - tower_delete (dev); |
|---|
| 862 | + tower_delete(dev); |
|---|
| 949 | 863 | } else { |
|---|
| 950 | 864 | dev->disconnected = 1; |
|---|
| 951 | 865 | /* wake up pollers */ |
|---|
| .. | .. |
|---|
| 962 | 876 | |
|---|
| 963 | 877 | MODULE_AUTHOR(DRIVER_AUTHOR); |
|---|
| 964 | 878 | MODULE_DESCRIPTION(DRIVER_DESC); |
|---|
| 965 | | -#ifdef MODULE_LICENSE |
|---|
| 966 | 879 | MODULE_LICENSE("GPL"); |
|---|
| 967 | | -#endif |
|---|