cdc-acm.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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_unregister(struct acm *acm)
  356. {
  357. tty_unregister_device(acm_tty_driver, acm->minor);
  358. usb_put_intf(acm->control);
  359. acm_table[acm->minor] = NULL;
  360. usb_free_urb(acm->ctrlurb);
  361. usb_free_urb(acm->readurb);
  362. usb_free_urb(acm->writeurb);
  363. kfree(acm);
  364. }
  365. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  366. {
  367. struct acm *acm = tty->driver_data;
  368. if (!acm || !acm->used)
  369. return;
  370. down(&open_sem);
  371. if (!--acm->used) {
  372. if (acm->dev) {
  373. acm_set_control(acm, acm->ctrlout = 0);
  374. usb_kill_urb(acm->ctrlurb);
  375. usb_kill_urb(acm->writeurb);
  376. usb_kill_urb(acm->readurb);
  377. } else
  378. acm_tty_unregister(acm);
  379. }
  380. up(&open_sem);
  381. }
  382. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  383. {
  384. struct acm *acm = tty->driver_data;
  385. int stat;
  386. unsigned long flags;
  387. int wbn;
  388. struct acm_wb *wb;
  389. dbg("Entering acm_tty_write to write %d bytes,\n", count);
  390. if (!ACM_READY(acm))
  391. return -EINVAL;
  392. if (!count)
  393. return 0;
  394. spin_lock_irqsave(&acm->write_lock, flags);
  395. if ((wbn = acm_wb_alloc(acm)) < 0) {
  396. spin_unlock_irqrestore(&acm->write_lock, flags);
  397. acm_write_start(acm);
  398. return 0;
  399. }
  400. wb = &acm->wb[wbn];
  401. count = (count > acm->writesize) ? acm->writesize : count;
  402. dbg("Get %d bytes...", count);
  403. memcpy(wb->buf, buf, count);
  404. wb->len = count;
  405. spin_unlock_irqrestore(&acm->write_lock, flags);
  406. if ((stat = acm_write_start(acm)) < 0)
  407. return stat;
  408. return count;
  409. }
  410. static int acm_tty_write_room(struct tty_struct *tty)
  411. {
  412. struct acm *acm = tty->driver_data;
  413. if (!ACM_READY(acm))
  414. return -EINVAL;
  415. /*
  416. * Do not let the line discipline to know that we have a reserve,
  417. * or it might get too enthusiastic.
  418. */
  419. return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
  420. }
  421. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  422. {
  423. struct acm *acm = tty->driver_data;
  424. if (!ACM_READY(acm))
  425. return -EINVAL;
  426. /*
  427. * This is inaccurate (overcounts), but it works.
  428. */
  429. return (ACM_NWB - acm_wb_is_avail(acm)) * acm->writesize;
  430. }
  431. static void acm_tty_throttle(struct tty_struct *tty)
  432. {
  433. struct acm *acm = tty->driver_data;
  434. if (!ACM_READY(acm))
  435. return;
  436. spin_lock_bh(&acm->throttle_lock);
  437. acm->throttle = 1;
  438. spin_unlock_bh(&acm->throttle_lock);
  439. }
  440. static void acm_tty_unthrottle(struct tty_struct *tty)
  441. {
  442. struct acm *acm = tty->driver_data;
  443. if (!ACM_READY(acm))
  444. return;
  445. spin_lock_bh(&acm->throttle_lock);
  446. acm->throttle = 0;
  447. spin_unlock_bh(&acm->throttle_lock);
  448. if (acm->resubmit_to_unthrottle) {
  449. acm->resubmit_to_unthrottle = 0;
  450. acm_read_bulk(acm->readurb, NULL);
  451. }
  452. }
  453. static void acm_tty_break_ctl(struct tty_struct *tty, int state)
  454. {
  455. struct acm *acm = tty->driver_data;
  456. if (!ACM_READY(acm))
  457. return;
  458. if (acm_send_break(acm, state ? 0xffff : 0))
  459. dbg("send break failed");
  460. }
  461. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  462. {
  463. struct acm *acm = tty->driver_data;
  464. if (!ACM_READY(acm))
  465. return -EINVAL;
  466. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  467. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  468. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  469. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  470. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  471. TIOCM_CTS;
  472. }
  473. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  474. unsigned int set, unsigned int clear)
  475. {
  476. struct acm *acm = tty->driver_data;
  477. unsigned int newctrl;
  478. if (!ACM_READY(acm))
  479. return -EINVAL;
  480. newctrl = acm->ctrlout;
  481. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  482. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  483. newctrl = (newctrl & ~clear) | set;
  484. if (acm->ctrlout == newctrl)
  485. return 0;
  486. return acm_set_control(acm, acm->ctrlout = newctrl);
  487. }
  488. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  489. {
  490. struct acm *acm = tty->driver_data;
  491. if (!ACM_READY(acm))
  492. return -EINVAL;
  493. return -ENOIOCTLCMD;
  494. }
  495. static __u32 acm_tty_speed[] = {
  496. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  497. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  498. 57600, 115200, 230400, 460800, 500000, 576000,
  499. 921600, 1000000, 1152000, 1500000, 2000000,
  500. 2500000, 3000000, 3500000, 4000000
  501. };
  502. static __u8 acm_tty_size[] = {
  503. 5, 6, 7, 8
  504. };
  505. static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
  506. {
  507. struct acm *acm = tty->driver_data;
  508. struct termios *termios = tty->termios;
  509. struct usb_cdc_line_coding newline;
  510. int newctrl = acm->ctrlout;
  511. if (!ACM_READY(acm))
  512. return;
  513. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  514. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  515. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  516. newline.bParityType = termios->c_cflag & PARENB ?
  517. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  518. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  519. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  520. if (!newline.dwDTERate) {
  521. newline.dwDTERate = acm->line.dwDTERate;
  522. newctrl &= ~ACM_CTRL_DTR;
  523. } else newctrl |= ACM_CTRL_DTR;
  524. if (newctrl != acm->ctrlout)
  525. acm_set_control(acm, acm->ctrlout = newctrl);
  526. if (memcmp(&acm->line, &newline, sizeof newline)) {
  527. memcpy(&acm->line, &newline, sizeof newline);
  528. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  529. newline.bCharFormat, newline.bParityType,
  530. newline.bDataBits);
  531. acm_set_line(acm, &acm->line);
  532. }
  533. }
  534. /*
  535. * USB probe and disconnect routines.
  536. */
  537. /* Little helper: write buffers free */
  538. static void acm_write_buffers_free(struct acm *acm)
  539. {
  540. int i;
  541. struct acm_wb *wb;
  542. for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
  543. usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
  544. }
  545. }
  546. /* Little helper: write buffers allocate */
  547. static int acm_write_buffers_alloc(struct acm *acm)
  548. {
  549. int i;
  550. struct acm_wb *wb;
  551. for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
  552. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  553. &wb->dmah);
  554. if (!wb->buf) {
  555. while (i != 0) {
  556. --i;
  557. --wb;
  558. usb_buffer_free(acm->dev, acm->writesize,
  559. wb->buf, wb->dmah);
  560. }
  561. return -ENOMEM;
  562. }
  563. }
  564. return 0;
  565. }
  566. static int acm_probe (struct usb_interface *intf,
  567. const struct usb_device_id *id)
  568. {
  569. struct usb_cdc_union_desc *union_header = NULL;
  570. char *buffer = intf->altsetting->extra;
  571. int buflen = intf->altsetting->extralen;
  572. struct usb_interface *control_interface;
  573. struct usb_interface *data_interface;
  574. struct usb_endpoint_descriptor *epctrl;
  575. struct usb_endpoint_descriptor *epread;
  576. struct usb_endpoint_descriptor *epwrite;
  577. struct usb_device *usb_dev = interface_to_usbdev(intf);
  578. struct acm *acm;
  579. int minor;
  580. int ctrlsize,readsize;
  581. u8 *buf;
  582. u8 ac_management_function = 0;
  583. u8 call_management_function = 0;
  584. int call_interface_num = -1;
  585. int data_interface_num;
  586. unsigned long quirks;
  587. /* handle quirks deadly to normal probing*/
  588. quirks = (unsigned long)id->driver_info;
  589. if (quirks == NO_UNION_NORMAL) {
  590. data_interface = usb_ifnum_to_if(usb_dev, 1);
  591. control_interface = usb_ifnum_to_if(usb_dev, 0);
  592. goto skip_normal_probe;
  593. }
  594. /* normal probing*/
  595. if (!buffer) {
  596. err("Wierd descriptor references\n");
  597. return -EINVAL;
  598. }
  599. if (!buflen) {
  600. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  601. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  602. buflen = intf->cur_altsetting->endpoint->extralen;
  603. buffer = intf->cur_altsetting->endpoint->extra;
  604. } else {
  605. err("Zero length descriptor references\n");
  606. return -EINVAL;
  607. }
  608. }
  609. while (buflen > 0) {
  610. if (buffer [1] != USB_DT_CS_INTERFACE) {
  611. err("skipping garbage\n");
  612. goto next_desc;
  613. }
  614. switch (buffer [2]) {
  615. case USB_CDC_UNION_TYPE: /* we've found it */
  616. if (union_header) {
  617. err("More than one union descriptor, skipping ...");
  618. goto next_desc;
  619. }
  620. union_header = (struct usb_cdc_union_desc *)
  621. buffer;
  622. break;
  623. case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
  624. break; /* for now we ignore it */
  625. case USB_CDC_HEADER_TYPE: /* maybe check version */
  626. break; /* for now we ignore it */
  627. case USB_CDC_ACM_TYPE:
  628. ac_management_function = buffer[3];
  629. break;
  630. case USB_CDC_CALL_MANAGEMENT_TYPE:
  631. call_management_function = buffer[3];
  632. call_interface_num = buffer[4];
  633. if ((call_management_function & 3) != 3)
  634. err("This device cannot do calls on its own. It is no modem.");
  635. break;
  636. default:
  637. err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
  638. break;
  639. }
  640. next_desc:
  641. buflen -= buffer[0];
  642. buffer += buffer[0];
  643. }
  644. if (!union_header) {
  645. if (call_interface_num > 0) {
  646. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  647. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  648. control_interface = intf;
  649. } else {
  650. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  651. return -ENODEV;
  652. }
  653. } else {
  654. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  655. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  656. if (!control_interface || !data_interface) {
  657. dev_dbg(&intf->dev,"no interfaces\n");
  658. return -ENODEV;
  659. }
  660. }
  661. if (data_interface_num != call_interface_num)
  662. dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
  663. skip_normal_probe:
  664. /*workaround for switched interfaces */
  665. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  666. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  667. struct usb_interface *t;
  668. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  669. t = control_interface;
  670. control_interface = data_interface;
  671. data_interface = t;
  672. } else {
  673. return -EINVAL;
  674. }
  675. }
  676. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  677. dev_dbg(&intf->dev,"The data interface isn't available\n");
  678. return -EBUSY;
  679. }
  680. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  681. return -EINVAL;
  682. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  683. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  684. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  685. /* workaround for switched endpoints */
  686. if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
  687. /* descriptors are swapped */
  688. struct usb_endpoint_descriptor *t;
  689. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  690. t = epread;
  691. epread = epwrite;
  692. epwrite = t;
  693. }
  694. dbg("interfaces are valid");
  695. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  696. if (minor == ACM_TTY_MINORS) {
  697. err("no more free acm devices");
  698. return -ENODEV;
  699. }
  700. if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
  701. dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
  702. goto alloc_fail;
  703. }
  704. memset(acm, 0, sizeof(struct acm));
  705. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  706. readsize = le16_to_cpu(epread->wMaxPacketSize);
  707. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
  708. acm->control = control_interface;
  709. acm->data = data_interface;
  710. acm->minor = minor;
  711. acm->dev = usb_dev;
  712. acm->ctrl_caps = ac_management_function;
  713. acm->ctrlsize = ctrlsize;
  714. acm->readsize = readsize;
  715. acm->bh.func = acm_rx_tasklet;
  716. acm->bh.data = (unsigned long) acm;
  717. INIT_WORK(&acm->work, acm_softint, acm);
  718. spin_lock_init(&acm->throttle_lock);
  719. spin_lock_init(&acm->write_lock);
  720. acm->write_ready = 1;
  721. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  722. if (!buf) {
  723. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  724. goto alloc_fail2;
  725. }
  726. acm->ctrl_buffer = buf;
  727. buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
  728. if (!buf) {
  729. dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
  730. goto alloc_fail3;
  731. }
  732. acm->read_buffer = buf;
  733. if (acm_write_buffers_alloc(acm) < 0) {
  734. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  735. goto alloc_fail4;
  736. }
  737. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  738. if (!acm->ctrlurb) {
  739. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  740. goto alloc_fail5;
  741. }
  742. acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
  743. if (!acm->readurb) {
  744. dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
  745. goto alloc_fail6;
  746. }
  747. acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  748. if (!acm->writeurb) {
  749. dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
  750. goto alloc_fail7;
  751. }
  752. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  753. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  754. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  755. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  756. usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
  757. acm->read_buffer, readsize, acm_read_bulk, acm);
  758. acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  759. acm->readurb->transfer_dma = acm->read_dma;
  760. usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  761. NULL, acm->writesize, acm_write_bulk, acm);
  762. acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  763. /* acm->writeurb->transfer_dma = 0; */
  764. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  765. acm_set_control(acm, acm->ctrlout);
  766. acm->line.dwDTERate = cpu_to_le32(9600);
  767. acm->line.bDataBits = 8;
  768. acm_set_line(acm, &acm->line);
  769. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  770. usb_get_intf(control_interface);
  771. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  772. acm_table[minor] = acm;
  773. usb_set_intfdata (intf, acm);
  774. return 0;
  775. alloc_fail7:
  776. usb_free_urb(acm->readurb);
  777. alloc_fail6:
  778. usb_free_urb(acm->ctrlurb);
  779. alloc_fail5:
  780. acm_write_buffers_free(acm);
  781. alloc_fail4:
  782. usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
  783. alloc_fail3:
  784. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  785. alloc_fail2:
  786. kfree(acm);
  787. alloc_fail:
  788. return -ENOMEM;
  789. }
  790. static void acm_disconnect(struct usb_interface *intf)
  791. {
  792. struct acm *acm = usb_get_intfdata (intf);
  793. struct usb_device *usb_dev = interface_to_usbdev(intf);
  794. if (!acm || !acm->dev) {
  795. dbg("disconnect on nonexisting interface");
  796. return;
  797. }
  798. down(&open_sem);
  799. acm->dev = NULL;
  800. usb_set_intfdata (intf, NULL);
  801. usb_kill_urb(acm->ctrlurb);
  802. usb_kill_urb(acm->readurb);
  803. usb_kill_urb(acm->writeurb);
  804. flush_scheduled_work(); /* wait for acm_softint */
  805. acm_write_buffers_free(acm);
  806. usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
  807. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  808. usb_driver_release_interface(&acm_driver, acm->data);
  809. if (!acm->used) {
  810. acm_tty_unregister(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. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  827. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  828. },
  829. /* control interfaces with various AT-command sets */
  830. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  831. USB_CDC_ACM_PROTO_AT_V25TER) },
  832. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  833. USB_CDC_ACM_PROTO_AT_PCCA101) },
  834. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  835. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  836. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  837. USB_CDC_ACM_PROTO_AT_GSM) },
  838. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  839. USB_CDC_ACM_PROTO_AT_3G ) },
  840. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  841. USB_CDC_ACM_PROTO_AT_CDMA) },
  842. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  843. { }
  844. };
  845. MODULE_DEVICE_TABLE (usb, acm_ids);
  846. static struct usb_driver acm_driver = {
  847. .owner = THIS_MODULE,
  848. .name = "cdc_acm",
  849. .probe = acm_probe,
  850. .disconnect = acm_disconnect,
  851. .id_table = acm_ids,
  852. };
  853. /*
  854. * TTY driver structures.
  855. */
  856. static struct tty_operations acm_ops = {
  857. .open = acm_tty_open,
  858. .close = acm_tty_close,
  859. .write = acm_tty_write,
  860. .write_room = acm_tty_write_room,
  861. .ioctl = acm_tty_ioctl,
  862. .throttle = acm_tty_throttle,
  863. .unthrottle = acm_tty_unthrottle,
  864. .chars_in_buffer = acm_tty_chars_in_buffer,
  865. .break_ctl = acm_tty_break_ctl,
  866. .set_termios = acm_tty_set_termios,
  867. .tiocmget = acm_tty_tiocmget,
  868. .tiocmset = acm_tty_tiocmset,
  869. };
  870. /*
  871. * Init / exit.
  872. */
  873. static int __init acm_init(void)
  874. {
  875. int retval;
  876. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  877. if (!acm_tty_driver)
  878. return -ENOMEM;
  879. acm_tty_driver->owner = THIS_MODULE,
  880. acm_tty_driver->driver_name = "acm",
  881. acm_tty_driver->name = "ttyACM",
  882. acm_tty_driver->devfs_name = "usb/acm/",
  883. acm_tty_driver->major = ACM_TTY_MAJOR,
  884. acm_tty_driver->minor_start = 0,
  885. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  886. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  887. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  888. acm_tty_driver->init_termios = tty_std_termios;
  889. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  890. tty_set_operations(acm_tty_driver, &acm_ops);
  891. retval = tty_register_driver(acm_tty_driver);
  892. if (retval) {
  893. put_tty_driver(acm_tty_driver);
  894. return retval;
  895. }
  896. retval = usb_register(&acm_driver);
  897. if (retval) {
  898. tty_unregister_driver(acm_tty_driver);
  899. put_tty_driver(acm_tty_driver);
  900. return retval;
  901. }
  902. info(DRIVER_VERSION ":" DRIVER_DESC);
  903. return 0;
  904. }
  905. static void __exit acm_exit(void)
  906. {
  907. usb_deregister(&acm_driver);
  908. tty_unregister_driver(acm_tty_driver);
  909. put_tty_driver(acm_tty_driver);
  910. }
  911. module_init(acm_init);
  912. module_exit(acm_exit);
  913. MODULE_AUTHOR( DRIVER_AUTHOR );
  914. MODULE_DESCRIPTION( DRIVER_DESC );
  915. MODULE_LICENSE("GPL");