cdc-acm.c 30 KB

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