cdc-acm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * cdc-acm.c
  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 <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. *
  10. * USB Abstract Control Model driver for USB modems and ISDN adapters
  11. *
  12. * Sponsored by SuSE
  13. *
  14. * ChangeLog:
  15. * v0.9 - thorough cleaning, URBification, almost a rewrite
  16. * v0.10 - some more cleanups
  17. * v0.11 - fixed flow control, read error doesn't stop reads
  18. * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
  19. * v0.13 - added termios, added hangup
  20. * v0.14 - sized down struct acm
  21. * v0.15 - fixed flow control again - characters could be lost
  22. * v0.16 - added code for modems with swapped data and control interfaces
  23. * v0.17 - added new style probing
  24. * v0.18 - fixed new style probing for devices with more configurations
  25. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  26. * v0.20 - switched to probing on interface (rather than device) class
  27. * v0.21 - revert to probing on device for devices with multiple configs
  28. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  29. * config we want, sysadmin changes bConfigurationValue in sysfs.
  30. * v0.23 - use softirq for rx processing, as needed by tty layer
  31. * v0.24 - change probe method to evaluate CDC union descriptor
  32. */
  33. /*
  34. * This program is free software; you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License as published by
  36. * the Free Software Foundation; either version 2 of the License, or
  37. * (at your option) any later version.
  38. *
  39. * This program is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. * GNU General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU General Public License
  45. * along with this program; if not, write to the Free Software
  46. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  47. */
  48. #undef DEBUG
  49. #include <linux/kernel.h>
  50. #include <linux/errno.h>
  51. #include <linux/init.h>
  52. #include <linux/slab.h>
  53. #include <linux/tty.h>
  54. #include <linux/tty_driver.h>
  55. #include <linux/tty_flip.h>
  56. #include <linux/module.h>
  57. #include <linux/smp_lock.h>
  58. #include <asm/uaccess.h>
  59. #include <linux/usb.h>
  60. #include <linux/usb_cdc.h>
  61. #include <asm/byteorder.h>
  62. #include <asm/unaligned.h>
  63. #include "cdc-acm.h"
  64. /*
  65. * Version Information
  66. */
  67. #define DRIVER_VERSION "v0.23"
  68. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
  69. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  70. static struct usb_driver acm_driver;
  71. static struct tty_driver *acm_tty_driver;
  72. static struct acm *acm_table[ACM_TTY_MINORS];
  73. static DECLARE_MUTEX(open_sem);
  74. #define ACM_READY(acm) (acm && acm->dev && acm->used)
  75. /*
  76. * Functions for ACM control messages.
  77. */
  78. static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
  79. {
  80. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  81. request, USB_RT_ACM, value,
  82. acm->control->altsetting[0].desc.bInterfaceNumber,
  83. buf, len, 5000);
  84. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
  85. return retval < 0 ? retval : 0;
  86. }
  87. /* devices aren't required to support these requests.
  88. * the cdc acm descriptor tells whether they do...
  89. */
  90. #define acm_set_control(acm, control) \
  91. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  92. #define acm_set_line(acm, line) \
  93. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  94. #define acm_send_break(acm, ms) \
  95. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  96. /*
  97. * Interrupt handlers for various ACM device responses
  98. */
  99. /* control interface reports status changes with "interrupt" transfers */
  100. static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
  101. {
  102. struct acm *acm = urb->context;
  103. struct usb_cdc_notification *dr = urb->transfer_buffer;
  104. unsigned char *data;
  105. int newctrl;
  106. int status;
  107. switch (urb->status) {
  108. case 0:
  109. /* success */
  110. break;
  111. case -ECONNRESET:
  112. case -ENOENT:
  113. case -ESHUTDOWN:
  114. /* this urb is terminated, clean up */
  115. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  116. return;
  117. default:
  118. dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
  119. goto exit;
  120. }
  121. if (!ACM_READY(acm))
  122. goto exit;
  123. data = (unsigned char *)(dr + 1);
  124. switch (dr->bNotificationType) {
  125. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  126. dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
  127. break;
  128. case USB_CDC_NOTIFY_SERIAL_STATE:
  129. newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
  130. if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  131. dbg("calling hangup");
  132. tty_hangup(acm->tty);
  133. }
  134. acm->ctrlin = newctrl;
  135. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  136. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  137. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  138. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  139. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  140. break;
  141. default:
  142. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  143. dr->bNotificationType, dr->wIndex,
  144. dr->wLength, data[0], data[1]);
  145. break;
  146. }
  147. exit:
  148. status = usb_submit_urb (urb, GFP_ATOMIC);
  149. if (status)
  150. err ("%s - usb_submit_urb failed with result %d",
  151. __FUNCTION__, status);
  152. }
  153. /* data interface returns incoming bytes, or we got unthrottled */
  154. static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
  155. {
  156. struct acm *acm = urb->context;
  157. dbg("Entering acm_read_bulk with status %d\n", urb->status);
  158. if (!ACM_READY(acm))
  159. return;
  160. if (urb->status)
  161. dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
  162. /* calling tty_flip_buffer_push() in_irq() isn't allowed */
  163. tasklet_schedule(&acm->bh);
  164. }
  165. static void acm_rx_tasklet(unsigned long _acm)
  166. {
  167. struct acm *acm = (void *)_acm;
  168. struct urb *urb = acm->readurb;
  169. struct tty_struct *tty = acm->tty;
  170. unsigned char *data = urb->transfer_buffer;
  171. int i = 0;
  172. dbg("Entering acm_rx_tasklet");
  173. if (urb->actual_length > 0 && !acm->throttle) {
  174. for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
  175. /* if we insert more than TTY_FLIPBUF_SIZE characters,
  176. * we drop them. */
  177. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  178. tty_flip_buffer_push(tty);
  179. }
  180. tty_insert_flip_char(tty, data[i], 0);
  181. }
  182. dbg("Handed %d bytes to tty layer", i+1);
  183. tty_flip_buffer_push(tty);
  184. }
  185. spin_lock(&acm->throttle_lock);
  186. if (acm->throttle) {
  187. dbg("Throtteling noticed");
  188. memmove(data, data + i, urb->actual_length - i);
  189. urb->actual_length -= i;
  190. acm->resubmit_to_unthrottle = 1;
  191. spin_unlock(&acm->throttle_lock);
  192. return;
  193. }
  194. spin_unlock(&acm->throttle_lock);
  195. urb->actual_length = 0;
  196. urb->dev = acm->dev;
  197. i = usb_submit_urb(urb, GFP_ATOMIC);
  198. if (i)
  199. dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
  200. }
  201. /* data interface wrote those outgoing bytes */
  202. static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
  203. {
  204. struct acm *acm = (struct acm *)urb->context;
  205. dbg("Entering acm_write_bulk with status %d\n", urb->status);
  206. if (!ACM_READY(acm))
  207. goto out;
  208. if (urb->status)
  209. dbg("nonzero write bulk status received: %d", urb->status);
  210. schedule_work(&acm->work);
  211. out:
  212. acm->ready_for_write = 1;
  213. }
  214. static void acm_softint(void *private)
  215. {
  216. struct acm *acm = private;
  217. dbg("Entering acm_softint.\n");
  218. if (!ACM_READY(acm))
  219. return;
  220. tty_wakeup(acm->tty);
  221. }
  222. /*
  223. * TTY handlers
  224. */
  225. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  226. {
  227. struct acm *acm;
  228. int rv = -EINVAL;
  229. dbg("Entering acm_tty_open.\n");
  230. down(&open_sem);
  231. acm = acm_table[tty->index];
  232. if (!acm || !acm->dev)
  233. goto err_out;
  234. else
  235. rv = 0;
  236. tty->driver_data = acm;
  237. acm->tty = tty;
  238. if (acm->used++) {
  239. goto done;
  240. }
  241. acm->ctrlurb->dev = acm->dev;
  242. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  243. dbg("usb_submit_urb(ctrl irq) failed");
  244. goto bail_out;
  245. }
  246. acm->readurb->dev = acm->dev;
  247. if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
  248. dbg("usb_submit_urb(read bulk) failed");
  249. goto bail_out_and_unlink;
  250. }
  251. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
  252. goto full_bailout;
  253. /* force low_latency on so that our tty_push actually forces the data through,
  254. otherwise it is scheduled, and with high data rates data can get lost. */
  255. tty->low_latency = 1;
  256. done:
  257. err_out:
  258. up(&open_sem);
  259. return rv;
  260. full_bailout:
  261. usb_kill_urb(acm->readurb);
  262. bail_out_and_unlink:
  263. usb_kill_urb(acm->ctrlurb);
  264. bail_out:
  265. acm->used--;
  266. up(&open_sem);
  267. return -EIO;
  268. }
  269. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  270. {
  271. struct acm *acm = tty->driver_data;
  272. if (!acm || !acm->used)
  273. return;
  274. down(&open_sem);
  275. if (!--acm->used) {
  276. if (acm->dev) {
  277. acm_set_control(acm, acm->ctrlout = 0);
  278. usb_kill_urb(acm->ctrlurb);
  279. usb_kill_urb(acm->writeurb);
  280. usb_kill_urb(acm->readurb);
  281. } else {
  282. tty_unregister_device(acm_tty_driver, acm->minor);
  283. acm_table[acm->minor] = NULL;
  284. usb_free_urb(acm->ctrlurb);
  285. usb_free_urb(acm->readurb);
  286. usb_free_urb(acm->writeurb);
  287. kfree(acm);
  288. }
  289. }
  290. up(&open_sem);
  291. }
  292. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  293. {
  294. struct acm *acm = tty->driver_data;
  295. int stat;
  296. dbg("Entering acm_tty_write to write %d bytes,\n", count);
  297. if (!ACM_READY(acm))
  298. return -EINVAL;
  299. if (!acm->ready_for_write)
  300. return 0;
  301. if (!count)
  302. return 0;
  303. count = (count > acm->writesize) ? acm->writesize : count;
  304. dbg("Get %d bytes...", count);
  305. memcpy(acm->write_buffer, buf, count);
  306. dbg(" Successfully copied.\n");
  307. acm->writeurb->transfer_buffer_length = count;
  308. acm->writeurb->dev = acm->dev;
  309. acm->ready_for_write = 0;
  310. stat = usb_submit_urb(acm->writeurb, GFP_ATOMIC);
  311. if (stat < 0) {
  312. dbg("usb_submit_urb(write bulk) failed");
  313. acm->ready_for_write = 1;
  314. return stat;
  315. }
  316. return count;
  317. }
  318. static int acm_tty_write_room(struct tty_struct *tty)
  319. {
  320. struct acm *acm = tty->driver_data;
  321. if (!ACM_READY(acm))
  322. return -EINVAL;
  323. return !acm->ready_for_write ? 0 : acm->writesize;
  324. }
  325. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  326. {
  327. struct acm *acm = tty->driver_data;
  328. if (!ACM_READY(acm))
  329. return -EINVAL;
  330. return !acm->ready_for_write ? acm->writeurb->transfer_buffer_length : 0;
  331. }
  332. static void acm_tty_throttle(struct tty_struct *tty)
  333. {
  334. struct acm *acm = tty->driver_data;
  335. if (!ACM_READY(acm))
  336. return;
  337. spin_lock_bh(&acm->throttle_lock);
  338. acm->throttle = 1;
  339. spin_unlock_bh(&acm->throttle_lock);
  340. }
  341. static void acm_tty_unthrottle(struct tty_struct *tty)
  342. {
  343. struct acm *acm = tty->driver_data;
  344. if (!ACM_READY(acm))
  345. return;
  346. spin_lock_bh(&acm->throttle_lock);
  347. acm->throttle = 0;
  348. spin_unlock_bh(&acm->throttle_lock);
  349. if (acm->resubmit_to_unthrottle) {
  350. acm->resubmit_to_unthrottle = 0;
  351. acm_read_bulk(acm->readurb, NULL);
  352. }
  353. }
  354. static void acm_tty_break_ctl(struct tty_struct *tty, int state)
  355. {
  356. struct acm *acm = tty->driver_data;
  357. if (!ACM_READY(acm))
  358. return;
  359. if (acm_send_break(acm, state ? 0xffff : 0))
  360. dbg("send break failed");
  361. }
  362. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  363. {
  364. struct acm *acm = tty->driver_data;
  365. if (!ACM_READY(acm))
  366. return -EINVAL;
  367. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  368. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  369. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  370. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  371. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  372. TIOCM_CTS;
  373. }
  374. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  375. unsigned int set, unsigned int clear)
  376. {
  377. struct acm *acm = tty->driver_data;
  378. unsigned int newctrl;
  379. if (!ACM_READY(acm))
  380. return -EINVAL;
  381. newctrl = acm->ctrlout;
  382. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  383. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  384. newctrl = (newctrl & ~clear) | set;
  385. if (acm->ctrlout == newctrl)
  386. return 0;
  387. return acm_set_control(acm, acm->ctrlout = newctrl);
  388. }
  389. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  390. {
  391. struct acm *acm = tty->driver_data;
  392. if (!ACM_READY(acm))
  393. return -EINVAL;
  394. return -ENOIOCTLCMD;
  395. }
  396. static __u32 acm_tty_speed[] = {
  397. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  398. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  399. 57600, 115200, 230400, 460800, 500000, 576000,
  400. 921600, 1000000, 1152000, 1500000, 2000000,
  401. 2500000, 3000000, 3500000, 4000000
  402. };
  403. static __u8 acm_tty_size[] = {
  404. 5, 6, 7, 8
  405. };
  406. static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
  407. {
  408. struct acm *acm = tty->driver_data;
  409. struct termios *termios = tty->termios;
  410. struct usb_cdc_line_coding newline;
  411. int newctrl = acm->ctrlout;
  412. if (!ACM_READY(acm))
  413. return;
  414. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  415. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  416. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  417. newline.bParityType = termios->c_cflag & PARENB ?
  418. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  419. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  420. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  421. if (!newline.dwDTERate) {
  422. newline.dwDTERate = acm->line.dwDTERate;
  423. newctrl &= ~ACM_CTRL_DTR;
  424. } else newctrl |= ACM_CTRL_DTR;
  425. if (newctrl != acm->ctrlout)
  426. acm_set_control(acm, acm->ctrlout = newctrl);
  427. if (memcmp(&acm->line, &newline, sizeof newline)) {
  428. memcpy(&acm->line, &newline, sizeof newline);
  429. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  430. newline.bCharFormat, newline.bParityType,
  431. newline.bDataBits);
  432. acm_set_line(acm, &acm->line);
  433. }
  434. }
  435. /*
  436. * USB probe and disconnect routines.
  437. */
  438. static int acm_probe (struct usb_interface *intf,
  439. const struct usb_device_id *id)
  440. {
  441. struct usb_cdc_union_desc *union_header = NULL;
  442. char *buffer = intf->altsetting->extra;
  443. int buflen = intf->altsetting->extralen;
  444. struct usb_interface *control_interface;
  445. struct usb_interface *data_interface;
  446. struct usb_endpoint_descriptor *epctrl;
  447. struct usb_endpoint_descriptor *epread;
  448. struct usb_endpoint_descriptor *epwrite;
  449. struct usb_device *usb_dev = interface_to_usbdev(intf);
  450. struct acm *acm;
  451. int minor;
  452. int ctrlsize,readsize;
  453. u8 *buf;
  454. u8 ac_management_function = 0;
  455. u8 call_management_function = 0;
  456. int call_interface_num = -1;
  457. int data_interface_num;
  458. unsigned long quirks;
  459. /* handle quirks deadly to normal probing*/
  460. quirks = (unsigned long)id->driver_info;
  461. if (quirks == NO_UNION_NORMAL) {
  462. data_interface = usb_ifnum_to_if(usb_dev, 1);
  463. control_interface = usb_ifnum_to_if(usb_dev, 0);
  464. goto skip_normal_probe;
  465. }
  466. /* normal probing*/
  467. if (!buffer) {
  468. err("Wierd descriptor references\n");
  469. return -EINVAL;
  470. }
  471. if (!buflen) {
  472. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  473. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  474. buflen = intf->cur_altsetting->endpoint->extralen;
  475. buffer = intf->cur_altsetting->endpoint->extra;
  476. } else {
  477. err("Zero length descriptor references\n");
  478. return -EINVAL;
  479. }
  480. }
  481. while (buflen > 0) {
  482. if (buffer [1] != USB_DT_CS_INTERFACE) {
  483. err("skipping garbage\n");
  484. goto next_desc;
  485. }
  486. switch (buffer [2]) {
  487. case USB_CDC_UNION_TYPE: /* we've found it */
  488. if (union_header) {
  489. err("More than one union descriptor, skipping ...");
  490. goto next_desc;
  491. }
  492. union_header = (struct usb_cdc_union_desc *)
  493. buffer;
  494. break;
  495. case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
  496. break; /* for now we ignore it */
  497. case USB_CDC_HEADER_TYPE: /* maybe check version */
  498. break; /* for now we ignore it */
  499. case USB_CDC_ACM_TYPE:
  500. ac_management_function = buffer[3];
  501. break;
  502. case USB_CDC_CALL_MANAGEMENT_TYPE:
  503. call_management_function = buffer[3];
  504. call_interface_num = buffer[4];
  505. if ((call_management_function & 3) != 3)
  506. err("This device cannot do calls on its own. It is no modem.");
  507. break;
  508. default:
  509. err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
  510. break;
  511. }
  512. next_desc:
  513. buflen -= buffer[0];
  514. buffer += buffer[0];
  515. }
  516. if (!union_header) {
  517. if (call_interface_num > 0) {
  518. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  519. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  520. control_interface = intf;
  521. } else {
  522. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  523. return -ENODEV;
  524. }
  525. } else {
  526. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  527. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  528. if (!control_interface || !data_interface) {
  529. dev_dbg(&intf->dev,"no interfaces\n");
  530. return -ENODEV;
  531. }
  532. }
  533. if (data_interface_num != call_interface_num)
  534. dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
  535. skip_normal_probe:
  536. /*workaround for switched interfaces */
  537. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  538. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  539. struct usb_interface *t;
  540. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  541. t = control_interface;
  542. control_interface = data_interface;
  543. data_interface = t;
  544. } else {
  545. return -EINVAL;
  546. }
  547. }
  548. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  549. dev_dbg(&intf->dev,"The data interface isn't available\n");
  550. return -EBUSY;
  551. }
  552. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  553. return -EINVAL;
  554. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  555. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  556. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  557. /* workaround for switched endpoints */
  558. if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
  559. /* descriptors are swapped */
  560. struct usb_endpoint_descriptor *t;
  561. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  562. t = epread;
  563. epread = epwrite;
  564. epwrite = t;
  565. }
  566. dbg("interfaces are valid");
  567. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  568. if (minor == ACM_TTY_MINORS) {
  569. err("no more free acm devices");
  570. return -ENODEV;
  571. }
  572. if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
  573. dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
  574. goto alloc_fail;
  575. }
  576. memset(acm, 0, sizeof(struct acm));
  577. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  578. readsize = le16_to_cpu(epread->wMaxPacketSize);
  579. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
  580. acm->control = control_interface;
  581. acm->data = data_interface;
  582. acm->minor = minor;
  583. acm->dev = usb_dev;
  584. acm->ctrl_caps = ac_management_function;
  585. acm->ctrlsize = ctrlsize;
  586. acm->readsize = readsize;
  587. acm->bh.func = acm_rx_tasklet;
  588. acm->bh.data = (unsigned long) acm;
  589. INIT_WORK(&acm->work, acm_softint, acm);
  590. spin_lock_init(&acm->throttle_lock);
  591. acm->ready_for_write = 1;
  592. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  593. if (!buf) {
  594. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  595. goto alloc_fail2;
  596. }
  597. acm->ctrl_buffer = buf;
  598. buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
  599. if (!buf) {
  600. dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
  601. goto alloc_fail3;
  602. }
  603. acm->read_buffer = buf;
  604. buf = usb_buffer_alloc(usb_dev, acm->writesize, GFP_KERNEL, &acm->write_dma);
  605. if (!buf) {
  606. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  607. goto alloc_fail4;
  608. }
  609. acm->write_buffer = buf;
  610. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  611. if (!acm->ctrlurb) {
  612. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  613. goto alloc_fail5;
  614. }
  615. acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
  616. if (!acm->readurb) {
  617. dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
  618. goto alloc_fail6;
  619. }
  620. acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  621. if (!acm->writeurb) {
  622. dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
  623. goto alloc_fail7;
  624. }
  625. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  626. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  627. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  628. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  629. usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
  630. acm->read_buffer, readsize, acm_read_bulk, acm);
  631. acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  632. acm->readurb->transfer_dma = acm->read_dma;
  633. usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  634. acm->write_buffer, acm->writesize, acm_write_bulk, acm);
  635. acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  636. acm->writeurb->transfer_dma = acm->write_dma;
  637. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  638. acm_set_control(acm, acm->ctrlout);
  639. acm->line.dwDTERate = cpu_to_le32(9600);
  640. acm->line.bDataBits = 8;
  641. acm_set_line(acm, &acm->line);
  642. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  643. tty_register_device(acm_tty_driver, minor, &intf->dev);
  644. acm_table[minor] = acm;
  645. usb_set_intfdata (intf, acm);
  646. return 0;
  647. alloc_fail7:
  648. usb_free_urb(acm->readurb);
  649. alloc_fail6:
  650. usb_free_urb(acm->ctrlurb);
  651. alloc_fail5:
  652. usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
  653. alloc_fail4:
  654. usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
  655. alloc_fail3:
  656. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  657. alloc_fail2:
  658. kfree(acm);
  659. alloc_fail:
  660. return -ENOMEM;
  661. }
  662. static void acm_disconnect(struct usb_interface *intf)
  663. {
  664. struct acm *acm = usb_get_intfdata (intf);
  665. struct usb_device *usb_dev = interface_to_usbdev(intf);
  666. if (!acm || !acm->dev) {
  667. dbg("disconnect on nonexisting interface");
  668. return;
  669. }
  670. down(&open_sem);
  671. acm->dev = NULL;
  672. usb_set_intfdata (intf, NULL);
  673. usb_kill_urb(acm->ctrlurb);
  674. usb_kill_urb(acm->readurb);
  675. usb_kill_urb(acm->writeurb);
  676. flush_scheduled_work(); /* wait for acm_softint */
  677. usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
  678. usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
  679. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  680. usb_driver_release_interface(&acm_driver, acm->data);
  681. if (!acm->used) {
  682. tty_unregister_device(acm_tty_driver, acm->minor);
  683. acm_table[acm->minor] = NULL;
  684. usb_free_urb(acm->ctrlurb);
  685. usb_free_urb(acm->readurb);
  686. usb_free_urb(acm->writeurb);
  687. kfree(acm);
  688. up(&open_sem);
  689. return;
  690. }
  691. up(&open_sem);
  692. if (acm->tty)
  693. tty_hangup(acm->tty);
  694. }
  695. /*
  696. * USB driver structure.
  697. */
  698. static struct usb_device_id acm_ids[] = {
  699. /* quirky and broken devices */
  700. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  701. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  702. },
  703. /* control interfaces with various AT-command sets */
  704. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  705. USB_CDC_ACM_PROTO_AT_V25TER) },
  706. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  707. USB_CDC_ACM_PROTO_AT_PCCA101) },
  708. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  709. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  710. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  711. USB_CDC_ACM_PROTO_AT_GSM) },
  712. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  713. USB_CDC_ACM_PROTO_AT_3G ) },
  714. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  715. USB_CDC_ACM_PROTO_AT_CDMA) },
  716. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  717. { }
  718. };
  719. MODULE_DEVICE_TABLE (usb, acm_ids);
  720. static struct usb_driver acm_driver = {
  721. .owner = THIS_MODULE,
  722. .name = "cdc_acm",
  723. .probe = acm_probe,
  724. .disconnect = acm_disconnect,
  725. .id_table = acm_ids,
  726. };
  727. /*
  728. * TTY driver structures.
  729. */
  730. static struct tty_operations acm_ops = {
  731. .open = acm_tty_open,
  732. .close = acm_tty_close,
  733. .write = acm_tty_write,
  734. .write_room = acm_tty_write_room,
  735. .ioctl = acm_tty_ioctl,
  736. .throttle = acm_tty_throttle,
  737. .unthrottle = acm_tty_unthrottle,
  738. .chars_in_buffer = acm_tty_chars_in_buffer,
  739. .break_ctl = acm_tty_break_ctl,
  740. .set_termios = acm_tty_set_termios,
  741. .tiocmget = acm_tty_tiocmget,
  742. .tiocmset = acm_tty_tiocmset,
  743. };
  744. /*
  745. * Init / exit.
  746. */
  747. static int __init acm_init(void)
  748. {
  749. int retval;
  750. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  751. if (!acm_tty_driver)
  752. return -ENOMEM;
  753. acm_tty_driver->owner = THIS_MODULE,
  754. acm_tty_driver->driver_name = "acm",
  755. acm_tty_driver->name = "ttyACM",
  756. acm_tty_driver->devfs_name = "usb/acm/",
  757. acm_tty_driver->major = ACM_TTY_MAJOR,
  758. acm_tty_driver->minor_start = 0,
  759. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  760. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  761. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  762. acm_tty_driver->init_termios = tty_std_termios;
  763. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  764. tty_set_operations(acm_tty_driver, &acm_ops);
  765. retval = tty_register_driver(acm_tty_driver);
  766. if (retval) {
  767. put_tty_driver(acm_tty_driver);
  768. return retval;
  769. }
  770. retval = usb_register(&acm_driver);
  771. if (retval) {
  772. tty_unregister_driver(acm_tty_driver);
  773. put_tty_driver(acm_tty_driver);
  774. return retval;
  775. }
  776. info(DRIVER_VERSION ":" DRIVER_DESC);
  777. return 0;
  778. }
  779. static void __exit acm_exit(void)
  780. {
  781. usb_deregister(&acm_driver);
  782. tty_unregister_driver(acm_tty_driver);
  783. put_tty_driver(acm_tty_driver);
  784. }
  785. module_init(acm_init);
  786. module_exit(acm_exit);
  787. MODULE_AUTHOR( DRIVER_AUTHOR );
  788. MODULE_DESCRIPTION( DRIVER_DESC );
  789. MODULE_LICENSE("GPL");