char: lp: introduce list to save port number
PATCH 1/5 char: lp: introduce list to save port number
When we are registering lp in LP_PARPORT_AUTO mode, we are not keeping any record of the parallel port number to which lp is connecting. Add an array to save the port number to it.
--- drivers/char/lp.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/char/lp.c +4 -0
@@ -141,6 +141,7 @@static DEFINE_MUTEX (lp_mutex);static struct lp_struct lp_table[LP_NO ]; +static int port_num[LP_NO ];static unsigned int lp_count =0 ;static struct class *lp_class;
@@ -938,6 +939,7 @@ static int lp_register(int nr, struct parport *port) CONSOLE_LP, port->name); }#endif + port_num[nr] = port->number;return 0 ; }
@@ -1013,6 +1015,7 @@ static int __init lp_init(void)init_waitqueue_head (&lp_table[i].dataq);mutex_init (&lp_table[i].port_mutex); lp_table[i].timeout =10 *HZ ; + port_num[i] =-1 ; }if (register_chrdev (LP_MAJOR ,"lp" , &lp_fops)) {
@@ -1091,6 +1094,7 @@ static void lp_cleanup_module(void)for (offset =0 ; offset <LP_NO ; offset++) {if (lp_table[offset].dev ==NULL )continue ; + port_num[offset] =-1 ;parport_unregister_device (lp_table[offset].dev);device_destroy (lp_class,MKDEV (LP_MAJOR , offset)); }
Signature · 1 line
2.11.0
PATCH 2/5 char: lp: detach the device when parallel port is removed
When the parallel port is usb based and the lp attaches to it, we do get /dev/lp0, but when we remove the usb device and the parallel port is gone, we are still left with /dev/lp0.
Unregister the device properly in the detach routine based on the port number it has connected to.
--- drivers/char/lp.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/char/lp.c +10 -9
@@ -976,6 +976,8 @@ static void lp_attach(struct parport *port)static void lp_detach (struct parport *port) { +int n; +/* Write this some day. */ #ifdef CONFIG_LP_CONSOLE if (console_registered == port) {
@@ -983,6 +985,14 @@ static void lp_detach(struct parport *port) console_registered =NULL ; }#endif /* CONFIG_LP_CONSOLE */ + +for (n =0 ; n <LP_NO ; n++) { +if (port_num[n] == port->number) { + port_num[n] =-1 ; +device_destroy (lp_class,MKDEV (LP_MAJOR , n)); +parport_unregister_device (lp_table[n].dev); + } + } } static struct parport_driver lp_driver = {
@@ -1082,8 +1092,6 @@ static int __init lp_init_module(void)static void lp_cleanup_module (void ) { -unsigned int offset; -parport_unregister_driver (&lp_driver);#ifdef CONFIG_LP_CONSOLE
@@ -1091,13 +1099,6 @@ static void lp_cleanup_module(void)#endif unregister_chrdev (LP_MAJOR ,"lp" ); -for (offset =0 ; offset <LP_NO ; offset++) { -if (lp_table[offset].dev ==NULL ) -continue ; - port_num[offset] =-1 ; -parport_unregister_device (lp_table[offset].dev); -device_destroy (lp_class,MKDEV (LP_MAJOR , offset)); - }class_destroy (lp_class); }
Signature · 1 line
2.11.0
PATCH 3/5 char: lp: use first unused lp number while registering
In reply to Sudip MukherjeeWhen the parallel port is usb based and the lp attaches to it based on LP_PARPORT_AUTO, we do get /dev/lp0 and when we remove the usb device /dev/lp0 is unregistered. But if we now reconnect the usb device we get /dev/lp1, another disconnection and reconnection and we get /dev/lp2.
Use the port number array to find the first unused lp number and use that to register the lp device with the parallel port.
--- drivers/char/lp.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/lp.c +5 -1
@@ -958,7 +958,11 @@ static void lp_attach(struct parport *port)printk (KERN_INFO "lp: ignoring parallel port (max. %d)\n" ,LP_NO );return ; } -if (!lp_register (lp_count, port)) +for (i =0 ; i <LP_NO ; i++) +if (port_num[i] ==-1 ) +break ; + +if (!lp_register (i, port)) lp_count++;break ;
Signature · 1 line
2.11.0
PATCH 4/5 char: lp: properly count the lp devices
In reply to Sudip MukherjeeWhen the parallel port is usb based and the lp attaches to it based on LP_PARPORT_AUTO, we do get /dev/lp0 and when we remove the usb device /dev/lp0 is unregistered. If we now reconnect the usb device we get our /dev/lp0 back. But if we now disconnect and reconnect eight times we donot get any lp device and dmesg shows:
lp: ignoring parallel port (max. 8)
Decrement the lp_count when the device detaches as this represents the number of lp devices connected to the system.
--- drivers/char/lp.c | 1 + 1 file changed, 1 insertion(+)
drivers/char/lp.c +1 -0
@@ -993,6 +993,7 @@ static void lp_detach(struct parport *port)for (n =0 ; n <LP_NO ; n++) {if (port_num[n] == port->number) { port_num[n] =-1 ; + lp_count--;device_destroy (lp_class,MKDEV (LP_MAJOR , n));parport_unregister_device (lp_table[n].dev); }
Signature · 1 line
2.11.0
PATCH 5/5 char: lp: use new parport device model
In reply to Sudip MukherjeeModify lp driver to use the new parallel port device model.
--- drivers/char/lp.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/char/lp.c +9 -4
@@ -912,9 +912,13 @@ static int __init lp_setup(char *str)static int lp_register (int nr,struct parport *port) { - lp_table[nr].dev =parport_register_device (port,"lp" , - lp_preempt,NULL ,NULL ,0 , - (void *) &lp_table[nr]); +struct pardev_cb ppdev_cb; + +memset (&ppdev_cb,0 ,sizeof (ppdev_cb)); + ppdev_cb.preempt = lp_preempt; + ppdev_cb.private = &lp_table[nr]; + lp_table[nr].dev =parport_register_dev_model (port,"lp" , + &ppdev_cb, nr);if (lp_table[nr].dev ==NULL )return 1 ; lp_table[nr].flags |=LP_EXIST ;
@@ -1002,8 +1006,9 @@ static void lp_detach(struct parport *port)static struct parport_driver lp_driver = { .name ="lp" , - .attach = lp_attach, + .match_port = lp_attach, .detach = lp_detach, + .devmodel = true, };static int __init lp_init (void )
Signature · 1 line
2.11.0