File: /usr/src/linux/drivers/usb/acm.c

1     /*
2      * acm.c  Version 0.20
3      *
4      * Copyright (c) 1999 Armin Fuerst	<fuerst@in.tum.de>
5      * Copyright (c) 1999 Pavel Machek	<pavel@suse.cz>
6      * Copyright (c) 1999 Johannes Erdfelt	<jerdfelt@valinux.com>
7      * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
8      *
9      * USB Abstract Control Model driver for USB modems and ISDN adapters
10      *
11      * Sponsored by SuSE
12      *
13      * ChangeLog:
14      *	v0.9  - thorough cleaning, URBification, almost a rewrite
15      *	v0.10 - some more cleanups
16      *	v0.11 - fixed flow control, read error doesn't stop reads
17      *	v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
18      *	v0.13 - added termios, added hangup
19      *	v0.14 - sized down struct acm
20      *	v0.15 - fixed flow control again - characters could be lost
21      *	v0.16 - added code for modems with swapped data and control interfaces
22      *	v0.17 - added new style probing
23      *	v0.18 - fixed new style probing for devices with more configurations
24      *	v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
25      *      v0.20 - switched to probing on interface (rather than device) class
26      */
27     
28     /*
29      * This program is free software; you can redistribute it and/or modify
30      * it under the terms of the GNU General Public License as published by
31      * the Free Software Foundation; either version 2 of the License, or
32      * (at your option) any later version.
33      *
34      * This program is distributed in the hope that it will be useful,
35      * but WITHOUT ANY WARRANTY; without even the implied warranty of
36      * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37      * GNU General Public License for more details.
38      *
39      * You should have received a copy of the GNU General Public License
40      * along with this program; if not, write to the Free Software
41      * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42      */
43     
44     #include <linux/kernel.h>
45     #include <linux/sched.h>
46     #include <linux/signal.h>
47     #include <linux/errno.h>
48     #include <linux/poll.h>
49     #include <linux/init.h>
50     #include <linux/slab.h>
51     #include <linux/fcntl.h>
52     #include <linux/tty.h>
53     #include <linux/tty_driver.h>
54     #include <linux/tty_flip.h>
55     #include <linux/module.h>
56     #include <linux/smp_lock.h>
57     #undef DEBUG
58     #include <linux/usb.h>
59     
60     /*
61      * Version Information
62      */
63     #define DRIVER_VERSION "v0.20"
64     #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
65     #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
66     
67     /*
68      * CMSPAR, some architectures can't have space and mark parity.
69      */
70     
71     #ifndef CMSPAR
72     #define CMSPAR			0
73     #endif
74     
75     /*
76      * Major and minor numbers.
77      */
78     
79     #define ACM_TTY_MAJOR		166
80     #define ACM_TTY_MINORS		32
81     
82     /*
83      * Requests.
84      */
85     
86     #define USB_RT_ACM		(USB_TYPE_CLASS | USB_RECIP_INTERFACE)
87     
88     #define ACM_REQ_COMMAND		0x00
89     #define ACM_REQ_RESPONSE	0x01
90     #define ACM_REQ_SET_FEATURE	0x02
91     #define ACM_REQ_GET_FEATURE	0x03
92     #define ACM_REQ_CLEAR_FEATURE	0x04
93     
94     #define ACM_REQ_SET_LINE	0x20
95     #define ACM_REQ_GET_LINE	0x21
96     #define ACM_REQ_SET_CONTROL	0x22
97     #define ACM_REQ_SEND_BREAK	0x23
98     
99     /*
100      * IRQs.
101      */
102     
103     #define ACM_IRQ_NETWORK		0x00
104     #define ACM_IRQ_LINE_STATE	0x20
105     
106     /*
107      * Output control lines.
108      */
109     
110     #define ACM_CTRL_DTR		0x01
111     #define ACM_CTRL_RTS		0x02
112     
113     /*
114      * Input control lines and line errors.
115      */
116     
117     #define ACM_CTRL_DCD		0x01
118     #define ACM_CTRL_DSR		0x02
119     #define ACM_CTRL_BRK		0x04
120     #define ACM_CTRL_RI		0x08
121     
122     #define ACM_CTRL_FRAMING	0x10
123     #define ACM_CTRL_PARITY		0x20
124     #define ACM_CTRL_OVERRUN	0x40
125     
126     /*
127      * Line speed and caracter encoding.
128      */
129     
130     struct acm_line {
131     	__u32 speed;
132     	__u8 stopbits;
133     	__u8 parity;
134     	__u8 databits;
135     } __attribute__ ((packed));
136     
137     /*
138      * Internal driver structures.
139      */
140     
141     struct acm {
142     	struct usb_device *dev;				/* the coresponding usb device */
143     	struct usb_interface *iface;			/* the interfaces - +0 control +1 data */
144     	struct tty_struct *tty;				/* the coresponding tty */
145     	struct urb ctrlurb, readurb, writeurb;		/* urbs */
146     	struct acm_line line;				/* line coding (bits, stop, parity) */
147     	struct tq_struct tqueue;			/* task queue for line discipline waking up */
148     	unsigned int ctrlin;				/* input control lines (DCD, DSR, RI, break, overruns) */
149     	unsigned int ctrlout;				/* output control lines (DTR, RTS) */
150     	unsigned int writesize;				/* max packet size for the output bulk endpoint */
151     	unsigned int used;				/* someone has this acm's device open */
152     	unsigned int minor;				/* acm minor number */
153     	unsigned char throttle;				/* throttled by tty layer */
154     	unsigned char clocal;				/* termios CLOCAL */
155     };
156     
157     static struct usb_driver acm_driver;
158     static struct tty_driver acm_tty_driver;
159     static struct acm *acm_table[ACM_TTY_MINORS];
160     
161     #define ACM_READY(acm)	(acm && acm->dev && acm->used)
162     
163     /*
164      * Functions for ACM control messages.
165      */
166     
167     static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
168     {
169     	int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
170     		request, USB_RT_ACM, value, acm->iface[0].altsetting[0].bInterfaceNumber, buf, len, HZ * 5);
171     	dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
172     	return retval < 0 ? retval : 0;
173     }
174     
175     #define acm_set_control(acm, control)	acm_ctrl_msg(acm, ACM_REQ_SET_CONTROL, control, NULL, 0)
176     #define acm_set_line(acm, line)		acm_ctrl_msg(acm, ACM_REQ_SET_LINE, 0, line, sizeof(struct acm_line))
177     #define acm_send_break(acm, ms)		acm_ctrl_msg(acm, ACM_REQ_SEND_BREAK, ms, NULL, 0)
178     
179     /*
180      * Interrupt handler for various ACM control events
181      */
182     
183     static void acm_ctrl_irq(struct urb *urb)
184     {
185     	struct acm *acm = urb->context;
186     	devrequest *dr = urb->transfer_buffer;
187     	unsigned char *data = (unsigned char *)(dr + 1);
188     	int newctrl;
189     
190     	if (!ACM_READY(acm)) return;
191     
192     	if (urb->status < 0) {
193     		dbg("nonzero ctrl irq status received: %d", urb->status);
194     		return;
195     	}
196     
197     	switch (dr->request) {
198     
199     		case ACM_IRQ_NETWORK:
200     
201     			dbg("%s network", data[0] ? "connected to" : "disconnected from");
202     			return;
203     
204     		case ACM_IRQ_LINE_STATE:
205     
206     			newctrl = le16_to_cpup((__u16 *) data);
207     
208     			if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
209     				dbg("calling hangup");
210     				tty_hangup(acm->tty);
211     			}
212     
213     			acm->ctrlin = newctrl;
214     
215     			dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
216     				acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',	acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
217     				acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',	acm->ctrlin & ACM_CTRL_RI  ? '+' : '-',
218     				acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',	acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
219     				acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
220     
221     			return;
222     
223     		default:
224     			dbg("unknown control event received: request %d index %d len %d data0 %d data1 %d",
225     				dr->request, dr->index, dr->length, data[0], data[1]);
226     			return;
227     	}
228     }
229     
230     static void acm_read_bulk(struct urb *urb)
231     {
232     	struct acm *acm = urb->context;
233     	struct tty_struct *tty = acm->tty;
234     	unsigned char *data = urb->transfer_buffer;
235     	int i = 0;
236     
237     	if (!ACM_READY(acm)) return;
238     
239     	if (urb->status)
240     		dbg("nonzero read bulk status received: %d", urb->status);
241     
242     	if (!urb->status & !acm->throttle)  {
243     		for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
244     			/* if we insert more than TTY_FLIPBUF_SIZE characters,
245     			 * we drop them. */
246     			if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
247     				tty_flip_buffer_push(tty);
248     			}
249     			tty_insert_flip_char(tty, data[i], 0);
250     		}
251     		tty_flip_buffer_push(tty);
252     	}
253     
254     	if (acm->throttle) {
255     		memmove(data, data + i, urb->actual_length - i);
256     		urb->actual_length -= i;
257     		return;
258     	}
259     
260     	urb->actual_length = 0;
261     	urb->dev = acm->dev;
262     
263     	if (usb_submit_urb(urb))
264     		dbg("failed resubmitting read urb");
265     }
266     
267     static void acm_write_bulk(struct urb *urb)
268     {
269     	struct acm *acm = (struct acm *)urb->context;
270     
271     	if (!ACM_READY(acm)) return;
272     
273     	if (urb->status)
274     		dbg("nonzero write bulk status received: %d", urb->status);
275     
276     	queue_task(&acm->tqueue, &tq_immediate);
277     	mark_bh(IMMEDIATE_BH);
278     }
279     
280     static void acm_softint(void *private)
281     {
282     	struct acm *acm = private;
283     	struct tty_struct *tty = acm->tty;
284     
285     	if (!ACM_READY(acm)) return;
286     
287     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
288     		(tty->ldisc.write_wakeup)(tty);
289     
290     	wake_up_interruptible(&tty->write_wait);
291     }
292     
293     /*
294      * TTY handlers
295      */
296     
297     static int acm_tty_open(struct tty_struct *tty, struct file *filp)
298     {
299     	struct acm *acm = acm_table[MINOR(tty->device)];
300     
301     	if (!acm || !acm->dev) return -EINVAL;
302     
303     	tty->driver_data = acm;
304     	acm->tty = tty;
305     
306     	MOD_INC_USE_COUNT;
307     
308             lock_kernel();
309     
310     	if (acm->used++) {
311                     unlock_kernel();
312                     return 0;
313             }
314     
315             unlock_kernel();
316     
317     	acm->ctrlurb.dev = acm->dev;
318     	if (usb_submit_urb(&acm->ctrlurb))
319     		dbg("usb_submit_urb(ctrl irq) failed");
320     
321     	acm->readurb.dev = acm->dev;
322     	if (usb_submit_urb(&acm->readurb))
323     		dbg("usb_submit_urb(read bulk) failed");
324     
325     	acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS);
326     
327     	/* force low_latency on so that our tty_push actually forces the data through, 
328     	   otherwise it is scheduled, and with high data rates data can get lost. */
329     	tty->low_latency = 1;
330     
331     	return 0;
332     }
333     
334     static void acm_tty_close(struct tty_struct *tty, struct file *filp)
335     {
336     	struct acm *acm = tty->driver_data;
337     
338     	if (!acm || !acm->used) return;
339     
340     	if (!--acm->used) {
341     		if (acm->dev) {
342     			acm_set_control(acm, acm->ctrlout = 0);
343     			usb_unlink_urb(&acm->ctrlurb);
344     			usb_unlink_urb(&acm->writeurb);
345     			usb_unlink_urb(&acm->readurb);
346     		} else {
347     			tty_unregister_devfs(&acm_tty_driver, acm->minor);
348     			acm_table[acm->minor] = NULL;
349     			kfree(acm);
350     		}
351     	}
352     	MOD_DEC_USE_COUNT;
353     }
354     
355     static int acm_tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
356     {
357     	struct acm *acm = tty->driver_data;
358     
359     	if (!ACM_READY(acm)) return -EINVAL;
360     	if (acm->writeurb.status == -EINPROGRESS) return 0;
361     	if (!count) return 0;
362     
363     	count = (count > acm->writesize) ? acm->writesize : count;
364     
365     	if (from_user)
366     		copy_from_user(acm->writeurb.transfer_buffer, buf, count);
367     	else
368     		memcpy(acm->writeurb.transfer_buffer, buf, count);
369     
370     	acm->writeurb.transfer_buffer_length = count;
371     	acm->writeurb.dev = acm->dev;
372     
373     	if (usb_submit_urb(&acm->writeurb))
374     		dbg("usb_submit_urb(write bulk) failed");
375     
376     	return count;
377     }
378     
379     static int acm_tty_write_room(struct tty_struct *tty)
380     {
381     	struct acm *acm = tty->driver_data;
382     	if (!ACM_READY(acm)) return -EINVAL;
383     	return acm->writeurb.status == -EINPROGRESS ? 0 : acm->writesize;
384     }
385     
386     static int acm_tty_chars_in_buffer(struct tty_struct *tty)
387     {
388     	struct acm *acm = tty->driver_data;
389     	if (!ACM_READY(acm)) return -EINVAL;
390     	return acm->writeurb.status == -EINPROGRESS ? acm->writeurb.transfer_buffer_length : 0;
391     }
392     
393     static void acm_tty_throttle(struct tty_struct *tty)
394     {
395     	struct acm *acm = tty->driver_data;
396     	if (!ACM_READY(acm)) return;
397     	acm->throttle = 1;
398     }
399     
400     static void acm_tty_unthrottle(struct tty_struct *tty)
401     {
402     	struct acm *acm = tty->driver_data;
403     	if (!ACM_READY(acm)) return;
404     	acm->throttle = 0;
405     	if (acm->readurb.status != -EINPROGRESS)
406     		acm_read_bulk(&acm->readurb);
407     }
408     
409     static void acm_tty_break_ctl(struct tty_struct *tty, int state)
410     {
411     	struct acm *acm = tty->driver_data;
412     	if (!ACM_READY(acm)) return;
413     	if (acm_send_break(acm, state ? 0xffff : 0))
414     		dbg("send break failed");
415     }
416     
417     static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
418     {
419     	struct acm *acm = tty->driver_data;
420     	unsigned int mask, newctrl;
421     
422     	if (!ACM_READY(acm)) return -EINVAL;
423     
424     	switch (cmd) {
425     
426     		case TIOCMGET:
427     
428     			return put_user((acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
429     				(acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
430     				(acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
431     				(acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
432     				(acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
433     				 TIOCM_CTS, (unsigned long *) arg);
434     
435     		case TIOCMSET:
436     		case TIOCMBIS:
437     		case TIOCMBIC:
438     
439     			if (get_user(mask, (unsigned long *) arg))
440     				return -EFAULT;
441     
442     			newctrl = acm->ctrlout;
443     			mask = (mask & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (mask & TIOCM_RTS ? ACM_CTRL_RTS : 0);
444     
445     			switch (cmd) {
446     				case TIOCMSET: newctrl  =  mask; break;
447     				case TIOCMBIS: newctrl |=  mask; break;
448     				case TIOCMBIC: newctrl &= ~mask; break;
449     			}
450     
451     			if (acm->ctrlout == newctrl) return 0;
452     			return acm_set_control(acm, acm->ctrlout = newctrl);
453     	}
454     
455     	return -ENOIOCTLCMD;
456     }
457     
458     static __u32 acm_tty_speed[] = {
459     	0, 50, 75, 110, 134, 150, 200, 300, 600,
460     	1200, 1800, 2400, 4800, 9600, 19200, 38400,
461     	57600, 115200, 230400, 460800, 500000, 576000,
462     	921600, 1000000, 1152000, 1500000, 2000000,
463     	2500000, 3000000, 3500000, 4000000
464     };
465     
466     static __u8 acm_tty_size[] = {
467     	5, 6, 7, 8
468     };
469     
470     static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
471     {
472     	struct acm *acm = tty->driver_data;
473     	struct termios *termios = tty->termios;
474     	struct acm_line newline;
475     	int newctrl = acm->ctrlout;
476     
477     	if (!ACM_READY(acm)) return;
478     
479     	newline.speed = cpu_to_le32p(acm_tty_speed +
480     		(termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
481     	newline.stopbits = termios->c_cflag & CSTOPB ? 2 : 0;
482     	newline.parity = termios->c_cflag & PARENB ?
483     		(termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
484     	newline.databits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
485     
486     	acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
487     
488     	if (!newline.speed) {
489     		newline.speed = acm->line.speed;
490     		newctrl &= ~ACM_CTRL_DTR;
491     	} else  newctrl |=  ACM_CTRL_DTR;
492     
493     	if (newctrl != acm->ctrlout)
494     		acm_set_control(acm, acm->ctrlout = newctrl);
495     
496     	if (memcmp(&acm->line, &newline, sizeof(struct acm_line))) {
497     		memcpy(&acm->line, &newline, sizeof(struct acm_line));
498     		dbg("set line: %d %d %d %d", newline.speed, newline.stopbits, newline.parity, newline.databits);
499     		acm_set_line(acm, &acm->line);
500     	}
501     }
502     
503     /*
504      * USB probe and disconnect routines.
505      */
506     
507     static void *acm_probe(struct usb_device *dev, unsigned int ifnum,
508     		       const struct usb_device_id *id)
509     {
510     	struct acm *acm;
511     	struct usb_config_descriptor *cfacm;
512     	struct usb_interface_descriptor *ifcom, *ifdata;
513     	struct usb_endpoint_descriptor *epctrl, *epread, *epwrite;
514     	int readsize, ctrlsize, minor, i;
515     	unsigned char *buf;
516     
517     	for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
518     
519     		cfacm = dev->config + i;
520     
521     		dbg("probing config %d", cfacm->bConfigurationValue);
522     
523     		if (cfacm->bNumInterfaces != 2 ||
524     		    usb_interface_claimed(cfacm->interface + 0) ||
525     		    usb_interface_claimed(cfacm->interface + 1))
526     			continue;
527     
528     		ifcom = cfacm->interface[0].altsetting + 0;
529     		ifdata = cfacm->interface[1].altsetting + 0;
530     
531     		if (ifdata->bInterfaceClass != 10 || ifdata->bNumEndpoints < 2) {
532     			ifcom = cfacm->interface[1].altsetting + 0;
533     			ifdata = cfacm->interface[0].altsetting + 0;
534     			if (ifdata->bInterfaceClass != 10 || ifdata->bNumEndpoints < 2)
535     				continue;
536     		}
537     
538     		if (ifcom->bInterfaceClass != 2 || ifcom->bInterfaceSubClass != 2 ||
539     		    ifcom->bInterfaceProtocol != 1 || ifcom->bNumEndpoints < 1)
540     			continue;
541     
542     		epctrl = ifcom->endpoint + 0;
543     		epread = ifdata->endpoint + 0;
544     		epwrite = ifdata->endpoint + 1;
545     
546     		if ((epctrl->bEndpointAddress & 0x80) != 0x80 || (epctrl->bmAttributes & 3) != 3 ||
547     		   (epread->bmAttributes & 3) != 2 || (epwrite->bmAttributes & 3) != 2 ||
548     		   ((epread->bEndpointAddress & 0x80) ^ (epwrite->bEndpointAddress & 0x80)) != 0x80)
549     			continue;
550     
551     		if ((epread->bEndpointAddress & 0x80) != 0x80) {
552     			epread = ifdata->endpoint + 1;
553     			epwrite = ifdata->endpoint + 0;
554     		}
555     
556     		usb_set_configuration(dev, cfacm->bConfigurationValue);
557     
558     		for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
559     		if (acm_table[minor]) {
560     			err("no more free acm devices");
561     			return NULL;
562     		}
563     
564     		if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
565     			err("out of memory");
566     			return NULL;
567     		}
568     		memset(acm, 0, sizeof(struct acm));
569     
570     		ctrlsize = epctrl->wMaxPacketSize;
571     		readsize = epread->wMaxPacketSize;
572     		acm->writesize = epwrite->wMaxPacketSize;
573     		acm->iface = cfacm->interface;
574     		acm->minor = minor;
575     		acm->dev = dev;
576     
577     		acm->tqueue.routine = acm_softint;
578     		acm->tqueue.data = acm;
579     
580     		if (!(buf = kmalloc(ctrlsize + readsize + acm->writesize, GFP_KERNEL))) {
581     			err("out of memory");
582     			kfree(acm);
583     			return NULL;
584     		}
585     
586     		FILL_INT_URB(&acm->ctrlurb, dev, usb_rcvintpipe(dev, epctrl->bEndpointAddress),
587     			buf, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
588     
589     		FILL_BULK_URB(&acm->readurb, dev, usb_rcvbulkpipe(dev, epread->bEndpointAddress),
590     			buf += ctrlsize, readsize, acm_read_bulk, acm);
591     		acm->readurb.transfer_flags |= USB_NO_FSBR;
592     
593     		FILL_BULK_URB(&acm->writeurb, dev, usb_sndbulkpipe(dev, epwrite->bEndpointAddress),
594     			buf += readsize, acm->writesize, acm_write_bulk, acm);
595     		acm->writeurb.transfer_flags |= USB_NO_FSBR;
596     
597     		printk(KERN_INFO "ttyACM%d: USB ACM device\n", minor);
598     
599     		acm_set_control(acm, acm->ctrlout);
600     
601     		acm->line.speed = cpu_to_le32(9600);
602     		acm->line.databits = 8;
603     		acm_set_line(acm, &acm->line);
604     
605     		usb_driver_claim_interface(&acm_driver, acm->iface + 0, acm);
606     		usb_driver_claim_interface(&acm_driver, acm->iface + 1, acm);
607     
608     		tty_register_devfs(&acm_tty_driver, 0, minor);
609     		return acm_table[minor] = acm;
610     	}
611     
612     	return NULL;
613     }
614     
615     static void acm_disconnect(struct usb_device *dev, void *ptr)
616     {
617     	struct acm *acm = ptr;
618     
619     	if (!acm || !acm->dev) {
620     		dbg("disconnect on nonexisting interface");
621     		return;
622     	}
623     
624     	acm->dev = NULL;
625     
626     	usb_unlink_urb(&acm->ctrlurb);
627     	usb_unlink_urb(&acm->readurb);
628     	usb_unlink_urb(&acm->writeurb);
629     
630     	kfree(acm->ctrlurb.transfer_buffer);
631     
632     	usb_driver_release_interface(&acm_driver, acm->iface + 0);
633     	usb_driver_release_interface(&acm_driver, acm->iface + 1);
634     
635     	if (!acm->used) {
636     		tty_unregister_devfs(&acm_tty_driver, acm->minor);
637     		acm_table[acm->minor] = NULL;
638     		kfree(acm);
639     		return;
640     	}
641     
642     	if (acm->tty)
643     		tty_hangup(acm->tty);
644     }
645     
646     /*
647      * USB driver structure.
648      */
649     
650     static struct usb_device_id acm_ids[] = {
651     	{match_flags: (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
652     	bInterfaceClass: USB_CLASS_COMM, bInterfaceSubClass: 2},
653     	{ }
654     };
655     
656     MODULE_DEVICE_TABLE (usb, acm_ids);
657     
658     static struct usb_driver acm_driver = {
659     	name:		"acm",
660     	probe:		acm_probe,
661     	disconnect:	acm_disconnect,
662     	id_table:	acm_ids,
663     };
664     
665     /*
666      * TTY driver structures.
667      */
668     
669     static int acm_tty_refcount;
670     
671     static struct tty_struct *acm_tty_table[ACM_TTY_MINORS];
672     static struct termios *acm_tty_termios[ACM_TTY_MINORS];
673     static struct termios *acm_tty_termios_locked[ACM_TTY_MINORS];
674     
675     static struct tty_driver acm_tty_driver = {
676     	magic:			TTY_DRIVER_MAGIC,
677     	driver_name:		"acm",
678     	name:			"usb/acm/%d",
679     	major:			ACM_TTY_MAJOR,
680     	minor_start:		0,
681     	num:			ACM_TTY_MINORS,
682     	type:			TTY_DRIVER_TYPE_SERIAL,
683     	subtype:		SERIAL_TYPE_NORMAL,
684     	flags:			TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
685     
686     	refcount:		&acm_tty_refcount,
687     
688     	table:			acm_tty_table,
689     	termios:		acm_tty_termios,
690     	termios_locked:		acm_tty_termios_locked,
691     
692     	open:			acm_tty_open,
693     	close:			acm_tty_close,
694     	write:			acm_tty_write,
695     	write_room:		acm_tty_write_room,
696     	ioctl:			acm_tty_ioctl,
697     	throttle:		acm_tty_throttle,
698     	unthrottle:		acm_tty_unthrottle,
699     	chars_in_buffer:	acm_tty_chars_in_buffer,
700     	break_ctl:		acm_tty_break_ctl,
701     	set_termios:		acm_tty_set_termios
702     };
703     
704     /*
705      * Init / exit.
706      */
707     
708     static int __init acm_init(void)
709     {
710     	acm_tty_driver.init_termios =		tty_std_termios;
711     	acm_tty_driver.init_termios.c_cflag =	B9600 | CS8 | CREAD | HUPCL | CLOCAL;
712     
713     	if (tty_register_driver(&acm_tty_driver))
714     		return -1;
715     
716     	if (usb_register(&acm_driver) < 0) {
717     		tty_unregister_driver(&acm_tty_driver);
718     		return -1;
719     	}
720     
721     	info(DRIVER_VERSION ":" DRIVER_DESC);
722     
723     	return 0;
724     }
725     
726     static void __exit acm_exit(void)
727     {
728     	usb_deregister(&acm_driver);
729     	tty_unregister_driver(&acm_tty_driver);
730     }
731     
732     module_init(acm_init);
733     module_exit(acm_exit);
734     
735     MODULE_AUTHOR( DRIVER_AUTHOR );
736     MODULE_DESCRIPTION( DRIVER_DESC );
737     MODULE_LICENSE("GPL");
738     
739