cdc-acm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. * Write buffer management.
  98. * All of these assume proper locks taken by the caller.
  99. */
  100. static int acm_wb_alloc(struct acm *acm)
  101. {
  102. int i, wbn;
  103. struct acm_wb *wb;
  104. wbn = acm->write_current;
  105. i = 0;
  106. for (;;) {
  107. wb = &acm->wb[wbn];
  108. if (!wb->use) {
  109. wb->use = 1;
  110. return wbn;
  111. }
  112. wbn = (wbn + 1) % ACM_NWB;
  113. if (++i >= ACM_NWB)
  114. return -1;
  115. }
  116. }
  117. static void acm_wb_free(struct acm *acm, int wbn)
  118. {
  119. acm->wb[wbn].use = 0;
  120. }
  121. static int acm_wb_is_avail(struct acm *acm)
  122. {
  123. int i, n;
  124. n = 0;
  125. for (i = 0; i < ACM_NWB; i++) {
  126. if (!acm->wb[i].use)
  127. n++;
  128. }
  129. return n;
  130. }
  131. static inline int acm_wb_is_used(struct acm *acm, int wbn)
  132. {
  133. return acm->wb[wbn].use;
  134. }
  135. /*
  136. * Finish write.
  137. */
  138. static void acm_write_done(struct acm *acm)
  139. {
  140. unsigned long flags;
  141. int wbn;
  142. spin_lock_irqsave(&acm->write_lock, flags);
  143. acm->write_ready = 1;
  144. wbn = acm->write_current;
  145. acm_wb_free(acm, wbn);
  146. acm->write_current = (wbn + 1) % ACM_NWB;
  147. spin_unlock_irqrestore(&acm->write_lock, flags);
  148. }
  149. /*
  150. * Poke write.
  151. */
  152. static int acm_write_start(struct acm *acm)
  153. {
  154. unsigned long flags;
  155. int wbn;
  156. struct acm_wb *wb;
  157. int rc;
  158. spin_lock_irqsave(&acm->write_lock, flags);
  159. if (!acm->dev) {
  160. spin_unlock_irqrestore(&acm->write_lock, flags);
  161. return -ENODEV;
  162. }
  163. if (!acm->write_ready) {
  164. spin_unlock_irqrestore(&acm->write_lock, flags);
  165. return 0; /* A white lie */
  166. }
  167. wbn = acm->write_current;
  168. if (!acm_wb_is_used(acm, wbn)) {
  169. spin_unlock_irqrestore(&acm->write_lock, flags);
  170. return 0;
  171. }
  172. wb = &acm->wb[wbn];
  173. acm->write_ready = 0;
  174. spin_unlock_irqrestore(&acm->write_lock, flags);
  175. acm->writeurb->transfer_buffer = wb->buf;
  176. acm->writeurb->transfer_dma = wb->dmah;
  177. acm->writeurb->transfer_buffer_length = wb->len;
  178. acm->writeurb->dev = acm->dev;
  179. if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
  180. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  181. acm_write_done(acm);
  182. }
  183. return rc;
  184. }
  185. /*
  186. * Interrupt handlers for various ACM device responses
  187. */
  188. /* control interface reports status changes with "interrupt" transfers */
  189. static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
  190. {
  191. struct acm *acm = urb->context;
  192. struct usb_cdc_notification *dr = urb->transfer_buffer;
  193. unsigned char *data;
  194. int newctrl;
  195. int status;
  196. switch (urb->status) {
  197. case 0:
  198. /* success */
  199. break;
  200. case -ECONNRESET:
  201. case -ENOENT:
  202. case -ESHUTDOWN:
  203. /* this urb is terminated, clean up */
  204. dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
  205. return;
  206. default:
  207. dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
  208. goto exit;
  209. }
  210. if (!ACM_READY(acm))
  211. goto exit;
  212. data = (unsigned char *)(dr + 1);
  213. switch (dr->bNotificationType) {
  214. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  215. dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
  216. break;
  217. case USB_CDC_NOTIFY_SERIAL_STATE:
  218. newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
  219. if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  220. dbg("calling hangup");
  221. tty_hangup(acm->tty);
  222. }
  223. acm->ctrlin = newctrl;
  224. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  225. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  226. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  227. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  228. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  229. break;
  230. default:
  231. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  232. dr->bNotificationType, dr->wIndex,
  233. dr->wLength, data[0], data[1]);
  234. break;
  235. }
  236. exit:
  237. status = usb_submit_urb (urb, GFP_ATOMIC);
  238. if (status)
  239. err ("%s - usb_submit_urb failed with result %d",
  240. __FUNCTION__, status);
  241. }
  242. /* data interface returns incoming bytes, or we got unthrottled */
  243. static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
  244. {
  245. struct acm *acm = urb->context;
  246. dbg("Entering acm_read_bulk with status %d\n", urb->status);
  247. if (!ACM_READY(acm))
  248. return;
  249. if (urb->status)
  250. dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
  251. /* calling tty_flip_buffer_push() in_irq() isn't allowed */
  252. tasklet_schedule(&acm->bh);
  253. }
  254. static void acm_rx_tasklet(unsigned long _acm)
  255. {
  256. struct acm *acm = (void *)_acm;
  257. struct urb *urb = acm->readurb;
  258. struct tty_struct *tty = acm->tty;
  259. unsigned char *data = urb->transfer_buffer;
  260. int i = 0;
  261. dbg("Entering acm_rx_tasklet");
  262. if (urb->actual_length > 0 && !acm->throttle) {
  263. for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
  264. /* if we insert more than TTY_FLIPBUF_SIZE characters,
  265. * we drop them. */
  266. if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
  267. tty_flip_buffer_push(tty);
  268. }
  269. tty_insert_flip_char(tty, data[i], 0);
  270. }
  271. dbg("Handed %d bytes to tty layer", i+1);
  272. tty_flip_buffer_push(tty);
  273. }
  274. spin_lock(&acm->throttle_lock);
  275. if (acm->throttle) {
  276. dbg("Throtteling noticed");
  277. memmove(data, data + i, urb->actual_length - i);
  278. urb->actual_length -= i;
  279. acm->resubmit_to_unthrottle = 1;
  280. spin_unlock(&acm->throttle_lock);
  281. return;
  282. }
  283. spin_unlock(&acm->throttle_lock);
  284. urb->actual_length = 0;
  285. urb->dev = acm->dev;
  286. i = usb_submit_urb(urb, GFP_ATOMIC);
  287. if (i)
  288. dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
  289. }
  290. /* data interface wrote those outgoing bytes */
  291. static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
  292. {
  293. struct acm *acm = (struct acm *)urb->context;
  294. dbg("Entering acm_write_bulk with status %d\n", urb->status);
  295. acm_write_done(acm);
  296. acm_write_start(acm);
  297. if (ACM_READY(acm))
  298. schedule_work(&acm->work);
  299. }
  300. static void acm_softint(void *private)
  301. {
  302. struct acm *acm = private;
  303. dbg("Entering acm_softint.\n");
  304. if (!ACM_READY(acm))
  305. return;
  306. tty_wakeup(acm->tty);
  307. }
  308. /*
  309. * TTY handlers
  310. */
  311. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  312. {
  313. struct acm *acm;
  314. int rv = -EINVAL;
  315. dbg("Entering acm_tty_open.\n");
  316. down(&open_sem);
  317. acm = acm_table[tty->index];
  318. if (!acm || !acm->dev)
  319. goto err_out;
  320. else
  321. rv = 0;
  322. tty->driver_data = acm;
  323. acm->tty = tty;
  324. if (acm->used++) {
  325. goto done;
  326. }
  327. acm->ctrlurb->dev = acm->dev;
  328. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  329. dbg("usb_submit_urb(ctrl irq) failed");
  330. goto bail_out;
  331. }
  332. acm->readurb->dev = acm->dev;
  333. if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
  334. dbg("usb_submit_urb(read bulk) failed");
  335. goto bail_out_and_unlink;
  336. }
  337. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
  338. goto full_bailout;
  339. /* force low_latency on so that our tty_push actually forces the data through,
  340. otherwise it is scheduled, and with high data rates data can get lost. */
  341. tty->low_latency = 1;
  342. done:
  343. err_out:
  344. up(&open_sem);
  345. return rv;
  346. full_bailout:
  347. usb_kill_urb(acm->readurb);
  348. bail_out_and_unlink:
  349. usb_kill_urb(acm->ctrlurb);
  350. bail_out:
  351. acm->used--;
  352. up(&open_sem);
  353. return -EIO;
  354. }
  355. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  356. {
  357. struct acm *acm = tty->driver_data;
  358. if (!acm || !acm->used)
  359. return;
  360. down(&open_sem);
  361. if (!--acm->used) {
  362. if (acm->dev) {
  363. acm_set_control(acm, acm->ctrlout = 0);
  364. usb_kill_urb(acm->ctrlurb);
  365. usb_kill_urb(acm->writeurb);
  366. usb_kill_urb(acm->readurb);
  367. } else {
  368. tty_unregister_device(acm_tty_driver, acm->minor);
  369. acm_table[acm->minor] = NULL;
  370. usb_free_urb(acm->ctrlurb);
  371. usb_free_urb(acm->readurb);
  372. usb_free_urb(acm->writeurb);
  373. kfree(acm);
  374. }
  375. }
  376. up(&open_sem);
  377. }
  378. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  379. {
  380. struct acm *acm = tty->driver_data;
  381. int stat;
  382. unsigned long flags;
  383. int wbn;
  384. struct acm_wb *wb;
  385. dbg("Entering acm_tty_write to write %d bytes,\n", count);
  386. if (!ACM_READY(acm))
  387. return -EINVAL;
  388. if (!count)
  389. return 0;
  390. spin_lock_irqsave(&acm->write_lock, flags);
  391. if ((wbn = acm_wb_alloc(acm)) < 0) {
  392. spin_unlock_irqrestore(&acm->write_lock, flags);
  393. acm_write_start(acm);
  394. return 0;
  395. }
  396. wb = &acm->wb[wbn];
  397. count = (count > acm->writesize) ? acm->writesize : count;
  398. dbg("Get %d bytes...", count);
  399. memcpy(wb->buf, buf, count);
  400. wb->len = count;
  401. spin_unlock_irqrestore(&acm->write_lock, flags);
  402. if ((stat = acm_write_start(acm)) < 0)
  403. return stat;
  404. return count;
  405. }
  406. static int acm_tty_write_room(struct tty_struct *tty)
  407. {
  408. struct acm *acm = tty->driver_data;
  409. if (!ACM_READY(acm))
  410. return -EINVAL;
  411. /*
  412. * Do not let the line discipline to know that we have a reserve,
  413. * or it might get too enthusiastic.
  414. */
  415. return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
  416. }
  417. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  418. {
  419. struct acm *acm = tty->driver_data;
  420. if (!ACM_READY(acm))
  421. return -EINVAL;
  422. /*
  423. * This is inaccurate (overcounts), but it works.
  424. */
  425. return (ACM_NWB - acm_wb_is_avail(acm)) * acm->writesize;
  426. }
  427. static void acm_tty_throttle(struct tty_struct *tty)
  428. {
  429. struct acm *acm = tty->driver_data;
  430. if (!ACM_READY(acm))
  431. return;
  432. spin_lock_bh(&acm->throttle_lock);
  433. acm->throttle = 1;
  434. spin_unlock_bh(&acm->throttle_lock);
  435. }
  436. static void acm_tty_unthrottle(struct tty_struct *tty)
  437. {
  438. struct acm *acm = tty->driver_data;
  439. if (!ACM_READY(acm))
  440. return;
  441. spin_lock_bh(&acm->throttle_lock);
  442. acm->throttle = 0;
  443. spin_unlock_bh(&acm->throttle_lock);
  444. if (acm->resubmit_to_unthrottle) {
  445. acm->resubmit_to_unthrottle = 0;
  446. acm_read_bulk(acm->readurb, NULL);
  447. }
  448. }
  449. static void acm_tty_break_ctl(struct tty_struct *tty, int state)
  450. {
  451. struct acm *acm = tty->driver_data;
  452. if (!ACM_READY(acm))
  453. return;
  454. if (acm_send_break(acm, state ? 0xffff : 0))
  455. dbg("send break failed");
  456. }
  457. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  458. {
  459. struct acm *acm = tty->driver_data;
  460. if (!ACM_READY(acm))
  461. return -EINVAL;
  462. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  463. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  464. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  465. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  466. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  467. TIOCM_CTS;
  468. }
  469. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  470. unsigned int set, unsigned int clear)
  471. {
  472. struct acm *acm = tty->driver_data;
  473. unsigned int newctrl;
  474. if (!ACM_READY(acm))
  475. return -EINVAL;
  476. newctrl = acm->ctrlout;
  477. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  478. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  479. newctrl = (newctrl & ~clear) | set;
  480. if (acm->ctrlout == newctrl)
  481. return 0;
  482. return acm_set_control(acm, acm->ctrlout = newctrl);
  483. }
  484. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  485. {
  486. struct acm *acm = tty->driver_data;
  487. if (!ACM_READY(acm))
  488. return -EINVAL;
  489. return -ENOIOCTLCMD;
  490. }
  491. static __u32 acm_tty_speed[] = {
  492. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  493. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  494. 57600, 115200, 230400, 460800, 500000, 576000,
  495. 921600, 1000000, 1152000, 1500000, 2000000,
  496. 2500000, 3000000, 3500000, 4000000
  497. };
  498. static __u8 acm_tty_size[] = {
  499. 5, 6, 7, 8
  500. };
  501. static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
  502. {
  503. struct acm *acm = tty->driver_data;
  504. struct termios *termios = tty->termios;
  505. struct usb_cdc_line_coding newline;
  506. int newctrl = acm->ctrlout;
  507. if (!ACM_READY(acm))
  508. return;
  509. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  510. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  511. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  512. newline.bParityType = termios->c_cflag & PARENB ?
  513. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  514. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  515. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  516. if (!newline.dwDTERate) {
  517. newline.dwDTERate = acm->line.dwDTERate;
  518. newctrl &= ~ACM_CTRL_DTR;
  519. } else newctrl |= ACM_CTRL_DTR;
  520. if (newctrl != acm->ctrlout)
  521. acm_set_control(acm, acm->ctrlout = newctrl);
  522. if (memcmp(&acm->line, &newline, sizeof newline)) {
  523. memcpy(&acm->line, &newline, sizeof newline);
  524. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  525. newline.bCharFormat, newline.bParityType,
  526. newline.bDataBits);
  527. acm_set_line(acm, &acm->line);
  528. }
  529. }
  530. /*
  531. * USB probe and disconnect routines.
  532. */
  533. /* Little helper: write buffers free */
  534. static void acm_write_buffers_free(struct acm *acm)
  535. {
  536. int i;
  537. struct acm_wb *wb;
  538. for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
  539. usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
  540. }
  541. }
  542. /* Little helper: write buffers allocate */
  543. static int acm_write_buffers_alloc(struct acm *acm)
  544. {
  545. int i;
  546. struct acm_wb *wb;
  547. for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
  548. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  549. &wb->dmah);
  550. if (!wb->buf) {
  551. while (i != 0) {
  552. --i;
  553. --wb;
  554. usb_buffer_free(acm->dev, acm->writesize,
  555. wb->buf, wb->dmah);
  556. }
  557. return -ENOMEM;
  558. }
  559. }
  560. return 0;
  561. }
  562. static int acm_probe (struct usb_interface *intf,
  563. const struct usb_device_id *id)
  564. {
  565. struct usb_cdc_union_desc *union_header = NULL;
  566. char *buffer = intf->altsetting->extra;
  567. int buflen = intf->altsetting->extralen;
  568. struct usb_interface *control_interface;
  569. struct usb_interface *data_interface;
  570. struct usb_endpoint_descriptor *epctrl;
  571. struct usb_endpoint_descriptor *epread;
  572. struct usb_endpoint_descriptor *epwrite;
  573. struct usb_device *usb_dev = interface_to_usbdev(intf);
  574. struct acm *acm;
  575. int minor;
  576. int ctrlsize,readsize;
  577. u8 *buf;
  578. u8 ac_management_function = 0;
  579. u8 call_management_function = 0;
  580. int call_interface_num = -1;
  581. int data_interface_num;
  582. unsigned long quirks;
  583. /* handle quirks deadly to normal probing*/
  584. quirks = (unsigned long)id->driver_info;
  585. if (quirks == NO_UNION_NORMAL) {
  586. data_interface = usb_ifnum_to_if(usb_dev, 1);
  587. control_interface = usb_ifnum_to_if(usb_dev, 0);
  588. goto skip_normal_probe;
  589. }
  590. /* normal probing*/
  591. if (!buffer) {
  592. err("Wierd descriptor references\n");
  593. return -EINVAL;
  594. }
  595. if (!buflen) {
  596. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  597. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  598. buflen = intf->cur_altsetting->endpoint->extralen;
  599. buffer = intf->cur_altsetting->endpoint->extra;
  600. } else {
  601. err("Zero length descriptor references\n");
  602. return -EINVAL;
  603. }
  604. }
  605. while (buflen > 0) {
  606. if (buffer [1] != USB_DT_CS_INTERFACE) {
  607. err("skipping garbage\n");
  608. goto next_desc;
  609. }
  610. switch (buffer [2]) {
  611. case USB_CDC_UNION_TYPE: /* we've found it */
  612. if (union_header) {
  613. err("More than one union descriptor, skipping ...");
  614. goto next_desc;
  615. }
  616. union_header = (struct usb_cdc_union_desc *)
  617. buffer;
  618. break;
  619. case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
  620. break; /* for now we ignore it */
  621. case USB_CDC_HEADER_TYPE: /* maybe check version */
  622. break; /* for now we ignore it */
  623. case USB_CDC_ACM_TYPE:
  624. ac_management_function = buffer[3];
  625. break;
  626. case USB_CDC_CALL_MANAGEMENT_TYPE:
  627. call_management_function = buffer[3];
  628. call_interface_num = buffer[4];
  629. if ((call_management_function & 3) != 3)
  630. err("This device cannot do calls on its own. It is no modem.");
  631. break;
  632. default:
  633. err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
  634. break;
  635. }
  636. next_desc:
  637. buflen -= buffer[0];
  638. buffer += buffer[0];
  639. }
  640. if (!union_header) {
  641. if (call_interface_num > 0) {
  642. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  643. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  644. control_interface = intf;
  645. } else {
  646. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  647. return -ENODEV;
  648. }
  649. } else {
  650. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  651. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  652. if (!control_interface || !data_interface) {
  653. dev_dbg(&intf->dev,"no interfaces\n");
  654. return -ENODEV;
  655. }
  656. }
  657. if (data_interface_num != call_interface_num)
  658. dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
  659. skip_normal_probe:
  660. /*workaround for switched interfaces */
  661. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  662. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  663. struct usb_interface *t;
  664. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  665. t = control_interface;
  666. control_interface = data_interface;
  667. data_interface = t;
  668. } else {
  669. return -EINVAL;
  670. }
  671. }
  672. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  673. dev_dbg(&intf->dev,"The data interface isn't available\n");
  674. return -EBUSY;
  675. }
  676. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  677. return -EINVAL;
  678. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  679. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  680. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  681. /* workaround for switched endpoints */
  682. if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
  683. /* descriptors are swapped */
  684. struct usb_endpoint_descriptor *t;
  685. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  686. t = epread;
  687. epread = epwrite;
  688. epwrite = t;
  689. }
  690. dbg("interfaces are valid");
  691. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  692. if (minor == ACM_TTY_MINORS) {
  693. err("no more free acm devices");
  694. return -ENODEV;
  695. }
  696. if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
  697. dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
  698. goto alloc_fail;
  699. }
  700. memset(acm, 0, sizeof(struct acm));
  701. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  702. readsize = le16_to_cpu(epread->wMaxPacketSize);
  703. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
  704. acm->control = control_interface;
  705. acm->data = data_interface;
  706. acm->minor = minor;
  707. acm->dev = usb_dev;
  708. acm->ctrl_caps = ac_management_function;
  709. acm->ctrlsize = ctrlsize;
  710. acm->readsize = readsize;
  711. acm->bh.func = acm_rx_tasklet;
  712. acm->bh.data = (unsigned long) acm;
  713. INIT_WORK(&acm->work, acm_softint, acm);
  714. spin_lock_init(&acm->throttle_lock);
  715. spin_lock_init(&acm->write_lock);
  716. acm->write_ready = 1;
  717. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  718. if (!buf) {
  719. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  720. goto alloc_fail2;
  721. }
  722. acm->ctrl_buffer = buf;
  723. buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
  724. if (!buf) {
  725. dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
  726. goto alloc_fail3;
  727. }
  728. acm->read_buffer = buf;
  729. if (acm_write_buffers_alloc(acm) < 0) {
  730. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  731. goto alloc_fail4;
  732. }
  733. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  734. if (!acm->ctrlurb) {
  735. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  736. goto alloc_fail5;
  737. }
  738. acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
  739. if (!acm->readurb) {
  740. dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
  741. goto alloc_fail6;
  742. }
  743. acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  744. if (!acm->writeurb) {
  745. dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
  746. goto alloc_fail7;
  747. }
  748. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  749. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  750. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  751. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  752. usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
  753. acm->read_buffer, readsize, acm_read_bulk, acm);
  754. acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  755. acm->readurb->transfer_dma = acm->read_dma;
  756. usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  757. NULL, acm->writesize, acm_write_bulk, acm);
  758. acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  759. /* acm->writeurb->transfer_dma = 0; */
  760. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  761. acm_set_control(acm, acm->ctrlout);
  762. acm->line.dwDTERate = cpu_to_le32(9600);
  763. acm->line.bDataBits = 8;
  764. acm_set_line(acm, &acm->line);
  765. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  766. tty_register_device(acm_tty_driver, minor, &intf->dev);
  767. acm_table[minor] = acm;
  768. usb_set_intfdata (intf, acm);
  769. return 0;
  770. alloc_fail7:
  771. usb_free_urb(acm->readurb);
  772. alloc_fail6:
  773. usb_free_urb(acm->ctrlurb);
  774. alloc_fail5:
  775. acm_write_buffers_free(acm);
  776. alloc_fail4:
  777. usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
  778. alloc_fail3:
  779. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  780. alloc_fail2:
  781. kfree(acm);
  782. alloc_fail:
  783. return -ENOMEM;
  784. }
  785. static void acm_disconnect(struct usb_interface *intf)
  786. {
  787. struct acm *acm = usb_get_intfdata (intf);
  788. struct usb_device *usb_dev = interface_to_usbdev(intf);
  789. if (!acm || !acm->dev) {
  790. dbg("disconnect on nonexisting interface");
  791. return;
  792. }
  793. down(&open_sem);
  794. acm->dev = NULL;
  795. usb_set_intfdata (intf, NULL);
  796. usb_kill_urb(acm->ctrlurb);
  797. usb_kill_urb(acm->readurb);
  798. usb_kill_urb(acm->writeurb);
  799. flush_scheduled_work(); /* wait for acm_softint */
  800. acm_write_buffers_free(acm);
  801. usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
  802. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  803. usb_driver_release_interface(&acm_driver, acm->data);
  804. if (!acm->used) {
  805. tty_unregister_device(acm_tty_driver, acm->minor);
  806. acm_table[acm->minor] = NULL;
  807. usb_free_urb(acm->ctrlurb);
  808. usb_free_urb(acm->readurb);
  809. usb_free_urb(acm->writeurb);
  810. kfree(acm);
  811. up(&open_sem);
  812. return;
  813. }
  814. up(&open_sem);
  815. if (acm->tty)
  816. tty_hangup(acm->tty);
  817. }
  818. /*
  819. * USB driver structure.
  820. */
  821. static struct usb_device_id acm_ids[] = {
  822. /* quirky and broken devices */
  823. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  824. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  825. },
  826. /* control interfaces with various AT-command sets */
  827. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  828. USB_CDC_ACM_PROTO_AT_V25TER) },
  829. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  830. USB_CDC_ACM_PROTO_AT_PCCA101) },
  831. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  832. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  833. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  834. USB_CDC_ACM_PROTO_AT_GSM) },
  835. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  836. USB_CDC_ACM_PROTO_AT_3G ) },
  837. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  838. USB_CDC_ACM_PROTO_AT_CDMA) },
  839. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  840. { }
  841. };
  842. MODULE_DEVICE_TABLE (usb, acm_ids);
  843. static struct usb_driver acm_driver = {
  844. .owner = THIS_MODULE,
  845. .name = "cdc_acm",
  846. .probe = acm_probe,
  847. .disconnect = acm_disconnect,
  848. .id_table = acm_ids,
  849. };
  850. /*
  851. * TTY driver structures.
  852. */
  853. static struct tty_operations acm_ops = {
  854. .open = acm_tty_open,
  855. .close = acm_tty_close,
  856. .write = acm_tty_write,
  857. .write_room = acm_tty_write_room,
  858. .ioctl = acm_tty_ioctl,
  859. .throttle = acm_tty_throttle,
  860. .unthrottle = acm_tty_unthrottle,
  861. .chars_in_buffer = acm_tty_chars_in_buffer,
  862. .break_ctl = acm_tty_break_ctl,
  863. .set_termios = acm_tty_set_termios,
  864. .tiocmget = acm_tty_tiocmget,
  865. .tiocmset = acm_tty_tiocmset,
  866. };
  867. /*
  868. * Init / exit.
  869. */
  870. static int __init acm_init(void)
  871. {
  872. int retval;
  873. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  874. if (!acm_tty_driver)
  875. return -ENOMEM;
  876. acm_tty_driver->owner = THIS_MODULE,
  877. acm_tty_driver->driver_name = "acm",
  878. acm_tty_driver->name = "ttyACM",
  879. acm_tty_driver->devfs_name = "usb/acm/",
  880. acm_tty_driver->major = ACM_TTY_MAJOR,
  881. acm_tty_driver->minor_start = 0,
  882. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  883. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  884. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  885. acm_tty_driver->init_termios = tty_std_termios;
  886. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  887. tty_set_operations(acm_tty_driver, &acm_ops);
  888. retval = tty_register_driver(acm_tty_driver);
  889. if (retval) {
  890. put_tty_driver(acm_tty_driver);
  891. return retval;
  892. }
  893. retval = usb_register(&acm_driver);
  894. if (retval) {
  895. tty_unregister_driver(acm_tty_driver);
  896. put_tty_driver(acm_tty_driver);
  897. return retval;
  898. }
  899. info(DRIVER_VERSION ":" DRIVER_DESC);
  900. return 0;
  901. }
  902. static void __exit acm_exit(void)
  903. {
  904. usb_deregister(&acm_driver);
  905. tty_unregister_driver(acm_tty_driver);
  906. put_tty_driver(acm_tty_driver);
  907. }
  908. module_init(acm_init);
  909. module_exit(acm_exit);
  910. MODULE_AUTHOR( DRIVER_AUTHOR );
  911. MODULE_DESCRIPTION( DRIVER_DESC );
  912. MODULE_LICENSE("GPL");