cdc-acm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@ucw.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
  20. * kmalloced
  21. * v0.13 - added termios, added hangup
  22. * v0.14 - sized down struct acm
  23. * v0.15 - fixed flow control again - characters could be lost
  24. * v0.16 - added code for modems with swapped data and control interfaces
  25. * v0.17 - added new style probing
  26. * v0.18 - fixed new style probing for devices with more configurations
  27. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  28. * v0.20 - switched to probing on interface (rather than device) class
  29. * v0.21 - revert to probing on device for devices with multiple configs
  30. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  31. * config we want, sysadmin changes bConfigurationValue in sysfs.
  32. * v0.23 - use softirq for rx processing, as needed by tty layer
  33. * v0.24 - change probe method to evaluate CDC union descriptor
  34. * v0.25 - downstream tasks paralelized to maximize throughput
  35. * v0.26 - multiple write urbs, writesize increased
  36. */
  37. /*
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation; either version 2 of the License, or
  41. * (at your option) any later version.
  42. *
  43. * This program is distributed in the hope that it will be useful,
  44. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. * GNU General Public License for more details.
  47. *
  48. * You should have received a copy of the GNU General Public License
  49. * along with this program; if not, write to the Free Software
  50. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  51. */
  52. #undef DEBUG
  53. #undef VERBOSE_DEBUG
  54. #include <linux/kernel.h>
  55. #include <linux/errno.h>
  56. #include <linux/init.h>
  57. #include <linux/slab.h>
  58. #include <linux/tty.h>
  59. #include <linux/serial.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/module.h>
  63. #include <linux/mutex.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/usb.h>
  66. #include <linux/usb/cdc.h>
  67. #include <asm/byteorder.h>
  68. #include <asm/unaligned.h>
  69. #include <linux/list.h>
  70. #include "cdc-acm.h"
  71. #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
  72. /*
  73. * Version Information
  74. */
  75. #define DRIVER_VERSION "v0.26"
  76. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  77. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  78. static struct usb_driver acm_driver;
  79. static struct tty_driver *acm_tty_driver;
  80. static struct acm *acm_table[ACM_TTY_MINORS];
  81. static DEFINE_MUTEX(open_mutex);
  82. #define ACM_READY(acm) (acm && acm->dev && acm->port.count)
  83. static const struct tty_port_operations acm_port_ops = {
  84. };
  85. #ifdef VERBOSE_DEBUG
  86. #define verbose 1
  87. #else
  88. #define verbose 0
  89. #endif
  90. /*
  91. * Functions for ACM control messages.
  92. */
  93. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  94. void *buf, int len)
  95. {
  96. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  97. request, USB_RT_ACM, value,
  98. acm->control->altsetting[0].desc.bInterfaceNumber,
  99. buf, len, 5000);
  100. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d",
  101. request, value, len, retval);
  102. return retval < 0 ? retval : 0;
  103. }
  104. /* devices aren't required to support these requests.
  105. * the cdc acm descriptor tells whether they do...
  106. */
  107. #define acm_set_control(acm, control) \
  108. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  109. #define acm_set_line(acm, line) \
  110. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  111. #define acm_send_break(acm, ms) \
  112. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  113. /*
  114. * Write buffer management.
  115. * All of these assume proper locks taken by the caller.
  116. */
  117. static int acm_wb_alloc(struct acm *acm)
  118. {
  119. int i, wbn;
  120. struct acm_wb *wb;
  121. wbn = 0;
  122. i = 0;
  123. for (;;) {
  124. wb = &acm->wb[wbn];
  125. if (!wb->use) {
  126. wb->use = 1;
  127. return wbn;
  128. }
  129. wbn = (wbn + 1) % ACM_NW;
  130. if (++i >= ACM_NW)
  131. return -1;
  132. }
  133. }
  134. static int acm_wb_is_avail(struct acm *acm)
  135. {
  136. int i, n;
  137. unsigned long flags;
  138. n = ACM_NW;
  139. spin_lock_irqsave(&acm->write_lock, flags);
  140. for (i = 0; i < ACM_NW; i++)
  141. n -= acm->wb[i].use;
  142. spin_unlock_irqrestore(&acm->write_lock, flags);
  143. return n;
  144. }
  145. /*
  146. * Finish write. Caller must hold acm->write_lock
  147. */
  148. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  149. {
  150. wb->use = 0;
  151. acm->transmitting--;
  152. usb_autopm_put_interface_async(acm->control);
  153. }
  154. /*
  155. * Poke write.
  156. *
  157. * the caller is responsible for locking
  158. */
  159. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  160. {
  161. int rc;
  162. acm->transmitting++;
  163. wb->urb->transfer_buffer = wb->buf;
  164. wb->urb->transfer_dma = wb->dmah;
  165. wb->urb->transfer_buffer_length = wb->len;
  166. wb->urb->dev = acm->dev;
  167. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  168. if (rc < 0) {
  169. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  170. acm_write_done(acm, wb);
  171. }
  172. return rc;
  173. }
  174. static int acm_write_start(struct acm *acm, int wbn)
  175. {
  176. unsigned long flags;
  177. struct acm_wb *wb = &acm->wb[wbn];
  178. int rc;
  179. spin_lock_irqsave(&acm->write_lock, flags);
  180. if (!acm->dev) {
  181. wb->use = 0;
  182. spin_unlock_irqrestore(&acm->write_lock, flags);
  183. return -ENODEV;
  184. }
  185. dbg("%s susp_count: %d", __func__, acm->susp_count);
  186. usb_autopm_get_interface_async(acm->control);
  187. if (acm->susp_count) {
  188. if (!acm->delayed_wb)
  189. acm->delayed_wb = wb;
  190. else
  191. usb_autopm_put_interface_async(acm->control);
  192. spin_unlock_irqrestore(&acm->write_lock, flags);
  193. return 0; /* A white lie */
  194. }
  195. usb_mark_last_busy(acm->dev);
  196. rc = acm_start_wb(acm, wb);
  197. spin_unlock_irqrestore(&acm->write_lock, flags);
  198. return rc;
  199. }
  200. /*
  201. * attributes exported through sysfs
  202. */
  203. static ssize_t show_caps
  204. (struct device *dev, struct device_attribute *attr, char *buf)
  205. {
  206. struct usb_interface *intf = to_usb_interface(dev);
  207. struct acm *acm = usb_get_intfdata(intf);
  208. return sprintf(buf, "%d", acm->ctrl_caps);
  209. }
  210. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  211. static ssize_t show_country_codes
  212. (struct device *dev, struct device_attribute *attr, char *buf)
  213. {
  214. struct usb_interface *intf = to_usb_interface(dev);
  215. struct acm *acm = usb_get_intfdata(intf);
  216. memcpy(buf, acm->country_codes, acm->country_code_size);
  217. return acm->country_code_size;
  218. }
  219. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  220. static ssize_t show_country_rel_date
  221. (struct device *dev, struct device_attribute *attr, char *buf)
  222. {
  223. struct usb_interface *intf = to_usb_interface(dev);
  224. struct acm *acm = usb_get_intfdata(intf);
  225. return sprintf(buf, "%d", acm->country_rel_date);
  226. }
  227. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  228. /*
  229. * Interrupt handlers for various ACM device responses
  230. */
  231. /* control interface reports status changes with "interrupt" transfers */
  232. static void acm_ctrl_irq(struct urb *urb)
  233. {
  234. struct acm *acm = urb->context;
  235. struct usb_cdc_notification *dr = urb->transfer_buffer;
  236. struct tty_struct *tty;
  237. unsigned char *data;
  238. int newctrl;
  239. int retval;
  240. int status = urb->status;
  241. switch (status) {
  242. case 0:
  243. /* success */
  244. break;
  245. case -ECONNRESET:
  246. case -ENOENT:
  247. case -ESHUTDOWN:
  248. /* this urb is terminated, clean up */
  249. dbg("%s - urb shutting down with status: %d", __func__, status);
  250. return;
  251. default:
  252. dbg("%s - nonzero urb status received: %d", __func__, status);
  253. goto exit;
  254. }
  255. if (!ACM_READY(acm))
  256. goto exit;
  257. data = (unsigned char *)(dr + 1);
  258. switch (dr->bNotificationType) {
  259. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  260. dbg("%s network", dr->wValue ?
  261. "connected to" : "disconnected from");
  262. break;
  263. case USB_CDC_NOTIFY_SERIAL_STATE:
  264. tty = tty_port_tty_get(&acm->port);
  265. newctrl = get_unaligned_le16(data);
  266. if (tty) {
  267. if (!acm->clocal &&
  268. (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  269. dbg("calling hangup");
  270. tty_hangup(tty);
  271. }
  272. tty_kref_put(tty);
  273. }
  274. acm->ctrlin = newctrl;
  275. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  276. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  277. acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  278. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  279. acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  280. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  281. acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  282. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  283. break;
  284. default:
  285. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  286. dr->bNotificationType, dr->wIndex,
  287. dr->wLength, data[0], data[1]);
  288. break;
  289. }
  290. exit:
  291. usb_mark_last_busy(acm->dev);
  292. retval = usb_submit_urb(urb, GFP_ATOMIC);
  293. if (retval)
  294. dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
  295. "result %d", __func__, retval);
  296. }
  297. /* data interface returns incoming bytes, or we got unthrottled */
  298. static void acm_read_bulk(struct urb *urb)
  299. {
  300. struct acm_rb *buf;
  301. struct acm_ru *rcv = urb->context;
  302. struct acm *acm = rcv->instance;
  303. int status = urb->status;
  304. dbg("Entering acm_read_bulk with status %d", status);
  305. if (!ACM_READY(acm)) {
  306. dev_dbg(&acm->data->dev, "Aborting, acm not ready");
  307. return;
  308. }
  309. usb_mark_last_busy(acm->dev);
  310. if (status)
  311. dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
  312. buf = rcv->buffer;
  313. buf->size = urb->actual_length;
  314. if (likely(status == 0)) {
  315. spin_lock(&acm->read_lock);
  316. acm->processing++;
  317. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  318. list_add_tail(&buf->list, &acm->filled_read_bufs);
  319. spin_unlock(&acm->read_lock);
  320. } else {
  321. /* we drop the buffer due to an error */
  322. spin_lock(&acm->read_lock);
  323. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  324. list_add(&buf->list, &acm->spare_read_bufs);
  325. spin_unlock(&acm->read_lock);
  326. /* nevertheless the tasklet must be kicked unconditionally
  327. so the queue cannot dry up */
  328. }
  329. if (likely(!acm->susp_count))
  330. tasklet_schedule(&acm->urb_task);
  331. }
  332. static void acm_rx_tasklet(unsigned long _acm)
  333. {
  334. struct acm *acm = (void *)_acm;
  335. struct acm_rb *buf;
  336. struct tty_struct *tty;
  337. struct acm_ru *rcv;
  338. unsigned long flags;
  339. unsigned char throttled;
  340. dbg("Entering acm_rx_tasklet");
  341. if (!ACM_READY(acm)) {
  342. dbg("acm_rx_tasklet: ACM not ready");
  343. return;
  344. }
  345. spin_lock_irqsave(&acm->throttle_lock, flags);
  346. throttled = acm->throttle;
  347. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  348. if (throttled) {
  349. dbg("acm_rx_tasklet: throttled");
  350. return;
  351. }
  352. tty = tty_port_tty_get(&acm->port);
  353. next_buffer:
  354. spin_lock_irqsave(&acm->read_lock, flags);
  355. if (list_empty(&acm->filled_read_bufs)) {
  356. spin_unlock_irqrestore(&acm->read_lock, flags);
  357. goto urbs;
  358. }
  359. buf = list_entry(acm->filled_read_bufs.next,
  360. struct acm_rb, list);
  361. list_del(&buf->list);
  362. spin_unlock_irqrestore(&acm->read_lock, flags);
  363. dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
  364. if (tty) {
  365. spin_lock_irqsave(&acm->throttle_lock, flags);
  366. throttled = acm->throttle;
  367. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  368. if (!throttled) {
  369. tty_insert_flip_string(tty, buf->base, buf->size);
  370. tty_flip_buffer_push(tty);
  371. } else {
  372. tty_kref_put(tty);
  373. dbg("Throttling noticed");
  374. spin_lock_irqsave(&acm->read_lock, flags);
  375. list_add(&buf->list, &acm->filled_read_bufs);
  376. spin_unlock_irqrestore(&acm->read_lock, flags);
  377. return;
  378. }
  379. }
  380. spin_lock_irqsave(&acm->read_lock, flags);
  381. list_add(&buf->list, &acm->spare_read_bufs);
  382. spin_unlock_irqrestore(&acm->read_lock, flags);
  383. goto next_buffer;
  384. urbs:
  385. tty_kref_put(tty);
  386. while (!list_empty(&acm->spare_read_bufs)) {
  387. spin_lock_irqsave(&acm->read_lock, flags);
  388. if (list_empty(&acm->spare_read_urbs)) {
  389. acm->processing = 0;
  390. spin_unlock_irqrestore(&acm->read_lock, flags);
  391. return;
  392. }
  393. rcv = list_entry(acm->spare_read_urbs.next,
  394. struct acm_ru, list);
  395. list_del(&rcv->list);
  396. spin_unlock_irqrestore(&acm->read_lock, flags);
  397. buf = list_entry(acm->spare_read_bufs.next,
  398. struct acm_rb, list);
  399. list_del(&buf->list);
  400. rcv->buffer = buf;
  401. if (acm->is_int_ep)
  402. usb_fill_int_urb(rcv->urb, acm->dev,
  403. acm->rx_endpoint,
  404. buf->base,
  405. acm->readsize,
  406. acm_read_bulk, rcv, acm->bInterval);
  407. else
  408. usb_fill_bulk_urb(rcv->urb, acm->dev,
  409. acm->rx_endpoint,
  410. buf->base,
  411. acm->readsize,
  412. acm_read_bulk, rcv);
  413. rcv->urb->transfer_dma = buf->dma;
  414. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  415. /* This shouldn't kill the driver as unsuccessful URBs are
  416. returned to the free-urbs-pool and resubmited ASAP */
  417. spin_lock_irqsave(&acm->read_lock, flags);
  418. if (acm->susp_count ||
  419. usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  420. list_add(&buf->list, &acm->spare_read_bufs);
  421. list_add(&rcv->list, &acm->spare_read_urbs);
  422. acm->processing = 0;
  423. spin_unlock_irqrestore(&acm->read_lock, flags);
  424. return;
  425. } else {
  426. spin_unlock_irqrestore(&acm->read_lock, flags);
  427. dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
  428. }
  429. }
  430. spin_lock_irqsave(&acm->read_lock, flags);
  431. acm->processing = 0;
  432. spin_unlock_irqrestore(&acm->read_lock, flags);
  433. }
  434. /* data interface wrote those outgoing bytes */
  435. static void acm_write_bulk(struct urb *urb)
  436. {
  437. struct acm_wb *wb = urb->context;
  438. struct acm *acm = wb->instance;
  439. unsigned long flags;
  440. if (verbose || urb->status
  441. || (urb->actual_length != urb->transfer_buffer_length))
  442. dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
  443. urb->actual_length,
  444. urb->transfer_buffer_length,
  445. urb->status);
  446. spin_lock_irqsave(&acm->write_lock, flags);
  447. acm_write_done(acm, wb);
  448. spin_unlock_irqrestore(&acm->write_lock, flags);
  449. if (ACM_READY(acm))
  450. schedule_work(&acm->work);
  451. else
  452. wake_up_interruptible(&acm->drain_wait);
  453. }
  454. static void acm_softint(struct work_struct *work)
  455. {
  456. struct acm *acm = container_of(work, struct acm, work);
  457. struct tty_struct *tty;
  458. dev_vdbg(&acm->data->dev, "tx work\n");
  459. if (!ACM_READY(acm))
  460. return;
  461. tty = tty_port_tty_get(&acm->port);
  462. tty_wakeup(tty);
  463. tty_kref_put(tty);
  464. }
  465. /*
  466. * TTY handlers
  467. */
  468. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  469. {
  470. struct acm *acm;
  471. int rv = -ENODEV;
  472. int i;
  473. dbg("Entering acm_tty_open.");
  474. mutex_lock(&open_mutex);
  475. acm = acm_table[tty->index];
  476. if (!acm || !acm->dev)
  477. goto out;
  478. else
  479. rv = 0;
  480. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  481. tty->driver_data = acm;
  482. tty_port_tty_set(&acm->port, tty);
  483. if (usb_autopm_get_interface(acm->control) < 0)
  484. goto early_bail;
  485. else
  486. acm->control->needs_remote_wakeup = 1;
  487. mutex_lock(&acm->mutex);
  488. if (acm->port.count++) {
  489. mutex_unlock(&acm->mutex);
  490. usb_autopm_put_interface(acm->control);
  491. goto out;
  492. }
  493. acm->ctrlurb->dev = acm->dev;
  494. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  495. dbg("usb_submit_urb(ctrl irq) failed");
  496. goto bail_out;
  497. }
  498. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  499. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  500. goto full_bailout;
  501. usb_autopm_put_interface(acm->control);
  502. INIT_LIST_HEAD(&acm->spare_read_urbs);
  503. INIT_LIST_HEAD(&acm->spare_read_bufs);
  504. INIT_LIST_HEAD(&acm->filled_read_bufs);
  505. for (i = 0; i < acm->rx_buflimit; i++)
  506. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  507. for (i = 0; i < acm->rx_buflimit; i++)
  508. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  509. acm->throttle = 0;
  510. set_bit(ASYNCB_INITIALIZED, &acm->port.flags);
  511. rv = tty_port_block_til_ready(&acm->port, tty, filp);
  512. tasklet_schedule(&acm->urb_task);
  513. mutex_unlock(&acm->mutex);
  514. out:
  515. mutex_unlock(&open_mutex);
  516. return rv;
  517. full_bailout:
  518. usb_kill_urb(acm->ctrlurb);
  519. bail_out:
  520. acm->port.count--;
  521. mutex_unlock(&acm->mutex);
  522. usb_autopm_put_interface(acm->control);
  523. early_bail:
  524. mutex_unlock(&open_mutex);
  525. tty_port_tty_set(&acm->port, NULL);
  526. return -EIO;
  527. }
  528. static void acm_tty_unregister(struct acm *acm)
  529. {
  530. int i, nr;
  531. nr = acm->rx_buflimit;
  532. tty_unregister_device(acm_tty_driver, acm->minor);
  533. usb_put_intf(acm->control);
  534. acm_table[acm->minor] = NULL;
  535. usb_free_urb(acm->ctrlurb);
  536. for (i = 0; i < ACM_NW; i++)
  537. usb_free_urb(acm->wb[i].urb);
  538. for (i = 0; i < nr; i++)
  539. usb_free_urb(acm->ru[i].urb);
  540. kfree(acm->country_codes);
  541. kfree(acm);
  542. }
  543. static int acm_tty_chars_in_buffer(struct tty_struct *tty);
  544. static void acm_port_down(struct acm *acm)
  545. {
  546. int i, nr = acm->rx_buflimit;
  547. mutex_lock(&open_mutex);
  548. if (acm->dev) {
  549. usb_autopm_get_interface(acm->control);
  550. acm_set_control(acm, acm->ctrlout = 0);
  551. usb_kill_urb(acm->ctrlurb);
  552. for (i = 0; i < ACM_NW; i++)
  553. usb_kill_urb(acm->wb[i].urb);
  554. tasklet_disable(&acm->urb_task);
  555. for (i = 0; i < nr; i++)
  556. usb_kill_urb(acm->ru[i].urb);
  557. tasklet_enable(&acm->urb_task);
  558. acm->control->needs_remote_wakeup = 0;
  559. usb_autopm_put_interface(acm->control);
  560. }
  561. mutex_unlock(&open_mutex);
  562. }
  563. static void acm_tty_hangup(struct tty_struct *tty)
  564. {
  565. struct acm *acm = tty->driver_data;
  566. tty_port_hangup(&acm->port);
  567. acm_port_down(acm);
  568. }
  569. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  570. {
  571. struct acm *acm = tty->driver_data;
  572. /* Perform the closing process and see if we need to do the hardware
  573. shutdown */
  574. if (!acm)
  575. return;
  576. if (tty_port_close_start(&acm->port, tty, filp) == 0) {
  577. mutex_lock(&open_mutex);
  578. if (!acm->dev) {
  579. tty_port_tty_set(&acm->port, NULL);
  580. acm_tty_unregister(acm);
  581. tty->driver_data = NULL;
  582. }
  583. mutex_unlock(&open_mutex);
  584. return;
  585. }
  586. acm_port_down(acm);
  587. tty_port_close_end(&acm->port, tty);
  588. tty_port_tty_set(&acm->port, NULL);
  589. }
  590. static int acm_tty_write(struct tty_struct *tty,
  591. const unsigned char *buf, int count)
  592. {
  593. struct acm *acm = tty->driver_data;
  594. int stat;
  595. unsigned long flags;
  596. int wbn;
  597. struct acm_wb *wb;
  598. dbg("Entering acm_tty_write to write %d bytes,", count);
  599. if (!ACM_READY(acm))
  600. return -EINVAL;
  601. if (!count)
  602. return 0;
  603. spin_lock_irqsave(&acm->write_lock, flags);
  604. wbn = acm_wb_alloc(acm);
  605. if (wbn < 0) {
  606. spin_unlock_irqrestore(&acm->write_lock, flags);
  607. return 0;
  608. }
  609. wb = &acm->wb[wbn];
  610. count = (count > acm->writesize) ? acm->writesize : count;
  611. dbg("Get %d bytes...", count);
  612. memcpy(wb->buf, buf, count);
  613. wb->len = count;
  614. spin_unlock_irqrestore(&acm->write_lock, flags);
  615. stat = acm_write_start(acm, wbn);
  616. if (stat < 0)
  617. return stat;
  618. return count;
  619. }
  620. static int acm_tty_write_room(struct tty_struct *tty)
  621. {
  622. struct acm *acm = tty->driver_data;
  623. if (!ACM_READY(acm))
  624. return -EINVAL;
  625. /*
  626. * Do not let the line discipline to know that we have a reserve,
  627. * or it might get too enthusiastic.
  628. */
  629. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  630. }
  631. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  632. {
  633. struct acm *acm = tty->driver_data;
  634. if (!ACM_READY(acm))
  635. return 0;
  636. /*
  637. * This is inaccurate (overcounts), but it works.
  638. */
  639. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  640. }
  641. static void acm_tty_throttle(struct tty_struct *tty)
  642. {
  643. struct acm *acm = tty->driver_data;
  644. if (!ACM_READY(acm))
  645. return;
  646. spin_lock_bh(&acm->throttle_lock);
  647. acm->throttle = 1;
  648. spin_unlock_bh(&acm->throttle_lock);
  649. }
  650. static void acm_tty_unthrottle(struct tty_struct *tty)
  651. {
  652. struct acm *acm = tty->driver_data;
  653. if (!ACM_READY(acm))
  654. return;
  655. spin_lock_bh(&acm->throttle_lock);
  656. acm->throttle = 0;
  657. spin_unlock_bh(&acm->throttle_lock);
  658. tasklet_schedule(&acm->urb_task);
  659. }
  660. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  661. {
  662. struct acm *acm = tty->driver_data;
  663. int retval;
  664. if (!ACM_READY(acm))
  665. return -EINVAL;
  666. retval = acm_send_break(acm, state ? 0xffff : 0);
  667. if (retval < 0)
  668. dbg("send break failed");
  669. return retval;
  670. }
  671. static int acm_tty_tiocmget(struct tty_struct *tty)
  672. {
  673. struct acm *acm = tty->driver_data;
  674. if (!ACM_READY(acm))
  675. return -EINVAL;
  676. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  677. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  678. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  679. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  680. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  681. TIOCM_CTS;
  682. }
  683. static int acm_tty_tiocmset(struct tty_struct *tty,
  684. unsigned int set, unsigned int clear)
  685. {
  686. struct acm *acm = tty->driver_data;
  687. unsigned int newctrl;
  688. if (!ACM_READY(acm))
  689. return -EINVAL;
  690. newctrl = acm->ctrlout;
  691. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  692. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  693. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  694. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  695. newctrl = (newctrl & ~clear) | set;
  696. if (acm->ctrlout == newctrl)
  697. return 0;
  698. return acm_set_control(acm, acm->ctrlout = newctrl);
  699. }
  700. static int acm_tty_ioctl(struct tty_struct *tty,
  701. unsigned int cmd, unsigned long arg)
  702. {
  703. struct acm *acm = tty->driver_data;
  704. if (!ACM_READY(acm))
  705. return -EINVAL;
  706. return -ENOIOCTLCMD;
  707. }
  708. static const __u32 acm_tty_speed[] = {
  709. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  710. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  711. 57600, 115200, 230400, 460800, 500000, 576000,
  712. 921600, 1000000, 1152000, 1500000, 2000000,
  713. 2500000, 3000000, 3500000, 4000000
  714. };
  715. static const __u8 acm_tty_size[] = {
  716. 5, 6, 7, 8
  717. };
  718. static void acm_tty_set_termios(struct tty_struct *tty,
  719. struct ktermios *termios_old)
  720. {
  721. struct acm *acm = tty->driver_data;
  722. struct ktermios *termios = tty->termios;
  723. struct usb_cdc_line_coding newline;
  724. int newctrl = acm->ctrlout;
  725. if (!ACM_READY(acm))
  726. return;
  727. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  728. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  729. newline.bParityType = termios->c_cflag & PARENB ?
  730. (termios->c_cflag & PARODD ? 1 : 2) +
  731. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  732. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  733. /* FIXME: Needs to clear unsupported bits in the termios */
  734. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  735. if (!newline.dwDTERate) {
  736. newline.dwDTERate = acm->line.dwDTERate;
  737. newctrl &= ~ACM_CTRL_DTR;
  738. } else
  739. newctrl |= ACM_CTRL_DTR;
  740. if (newctrl != acm->ctrlout)
  741. acm_set_control(acm, acm->ctrlout = newctrl);
  742. if (memcmp(&acm->line, &newline, sizeof newline)) {
  743. memcpy(&acm->line, &newline, sizeof newline);
  744. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  745. newline.bCharFormat, newline.bParityType,
  746. newline.bDataBits);
  747. acm_set_line(acm, &acm->line);
  748. }
  749. }
  750. /*
  751. * USB probe and disconnect routines.
  752. */
  753. /* Little helpers: write/read buffers free */
  754. static void acm_write_buffers_free(struct acm *acm)
  755. {
  756. int i;
  757. struct acm_wb *wb;
  758. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  759. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  760. usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
  761. }
  762. static void acm_read_buffers_free(struct acm *acm)
  763. {
  764. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  765. int i, n = acm->rx_buflimit;
  766. for (i = 0; i < n; i++)
  767. usb_free_coherent(usb_dev, acm->readsize,
  768. acm->rb[i].base, acm->rb[i].dma);
  769. }
  770. /* Little helper: write buffers allocate */
  771. static int acm_write_buffers_alloc(struct acm *acm)
  772. {
  773. int i;
  774. struct acm_wb *wb;
  775. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  776. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  777. &wb->dmah);
  778. if (!wb->buf) {
  779. while (i != 0) {
  780. --i;
  781. --wb;
  782. usb_free_coherent(acm->dev, acm->writesize,
  783. wb->buf, wb->dmah);
  784. }
  785. return -ENOMEM;
  786. }
  787. }
  788. return 0;
  789. }
  790. static int acm_probe(struct usb_interface *intf,
  791. const struct usb_device_id *id)
  792. {
  793. struct usb_cdc_union_desc *union_header = NULL;
  794. struct usb_cdc_country_functional_desc *cfd = NULL;
  795. unsigned char *buffer = intf->altsetting->extra;
  796. int buflen = intf->altsetting->extralen;
  797. struct usb_interface *control_interface;
  798. struct usb_interface *data_interface;
  799. struct usb_endpoint_descriptor *epctrl = NULL;
  800. struct usb_endpoint_descriptor *epread = NULL;
  801. struct usb_endpoint_descriptor *epwrite = NULL;
  802. struct usb_device *usb_dev = interface_to_usbdev(intf);
  803. struct acm *acm;
  804. int minor;
  805. int ctrlsize, readsize;
  806. u8 *buf;
  807. u8 ac_management_function = 0;
  808. u8 call_management_function = 0;
  809. int call_interface_num = -1;
  810. int data_interface_num;
  811. unsigned long quirks;
  812. int num_rx_buf;
  813. int i;
  814. int combined_interfaces = 0;
  815. /* normal quirks */
  816. quirks = (unsigned long)id->driver_info;
  817. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  818. /* handle quirks deadly to normal probing*/
  819. if (quirks == NO_UNION_NORMAL) {
  820. data_interface = usb_ifnum_to_if(usb_dev, 1);
  821. control_interface = usb_ifnum_to_if(usb_dev, 0);
  822. goto skip_normal_probe;
  823. }
  824. /* normal probing*/
  825. if (!buffer) {
  826. dev_err(&intf->dev, "Weird descriptor references\n");
  827. return -EINVAL;
  828. }
  829. if (!buflen) {
  830. if (intf->cur_altsetting->endpoint &&
  831. intf->cur_altsetting->endpoint->extralen &&
  832. intf->cur_altsetting->endpoint->extra) {
  833. dev_dbg(&intf->dev,
  834. "Seeking extra descriptors on endpoint\n");
  835. buflen = intf->cur_altsetting->endpoint->extralen;
  836. buffer = intf->cur_altsetting->endpoint->extra;
  837. } else {
  838. dev_err(&intf->dev,
  839. "Zero length descriptor references\n");
  840. return -EINVAL;
  841. }
  842. }
  843. while (buflen > 0) {
  844. if (buffer[1] != USB_DT_CS_INTERFACE) {
  845. dev_err(&intf->dev, "skipping garbage\n");
  846. goto next_desc;
  847. }
  848. switch (buffer[2]) {
  849. case USB_CDC_UNION_TYPE: /* we've found it */
  850. if (union_header) {
  851. dev_err(&intf->dev, "More than one "
  852. "union descriptor, skipping ...\n");
  853. goto next_desc;
  854. }
  855. union_header = (struct usb_cdc_union_desc *)buffer;
  856. break;
  857. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  858. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  859. break;
  860. case USB_CDC_HEADER_TYPE: /* maybe check version */
  861. break; /* for now we ignore it */
  862. case USB_CDC_ACM_TYPE:
  863. ac_management_function = buffer[3];
  864. break;
  865. case USB_CDC_CALL_MANAGEMENT_TYPE:
  866. call_management_function = buffer[3];
  867. call_interface_num = buffer[4];
  868. if ( (quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
  869. dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
  870. break;
  871. default:
  872. /* there are LOTS more CDC descriptors that
  873. * could legitimately be found here.
  874. */
  875. dev_dbg(&intf->dev, "Ignoring descriptor: "
  876. "type %02x, length %d\n",
  877. buffer[2], buffer[0]);
  878. break;
  879. }
  880. next_desc:
  881. buflen -= buffer[0];
  882. buffer += buffer[0];
  883. }
  884. if (!union_header) {
  885. if (call_interface_num > 0) {
  886. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  887. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  888. control_interface = intf;
  889. } else {
  890. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  891. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  892. return -ENODEV;
  893. } else {
  894. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  895. combined_interfaces = 1;
  896. control_interface = data_interface = intf;
  897. goto look_for_collapsed_interface;
  898. }
  899. }
  900. } else {
  901. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  902. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  903. if (!control_interface || !data_interface) {
  904. dev_dbg(&intf->dev, "no interfaces\n");
  905. return -ENODEV;
  906. }
  907. }
  908. if (data_interface_num != call_interface_num)
  909. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  910. if (control_interface == data_interface) {
  911. /* some broken devices designed for windows work this way */
  912. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  913. combined_interfaces = 1;
  914. /* a popular other OS doesn't use it */
  915. quirks |= NO_CAP_LINE;
  916. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  917. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  918. return -EINVAL;
  919. }
  920. look_for_collapsed_interface:
  921. for (i = 0; i < 3; i++) {
  922. struct usb_endpoint_descriptor *ep;
  923. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  924. if (usb_endpoint_is_int_in(ep))
  925. epctrl = ep;
  926. else if (usb_endpoint_is_bulk_out(ep))
  927. epwrite = ep;
  928. else if (usb_endpoint_is_bulk_in(ep))
  929. epread = ep;
  930. else
  931. return -EINVAL;
  932. }
  933. if (!epctrl || !epread || !epwrite)
  934. return -ENODEV;
  935. else
  936. goto made_compressed_probe;
  937. }
  938. skip_normal_probe:
  939. /*workaround for switched interfaces */
  940. if (data_interface->cur_altsetting->desc.bInterfaceClass
  941. != CDC_DATA_INTERFACE_TYPE) {
  942. if (control_interface->cur_altsetting->desc.bInterfaceClass
  943. == CDC_DATA_INTERFACE_TYPE) {
  944. struct usb_interface *t;
  945. dev_dbg(&intf->dev,
  946. "Your device has switched interfaces.\n");
  947. t = control_interface;
  948. control_interface = data_interface;
  949. data_interface = t;
  950. } else {
  951. return -EINVAL;
  952. }
  953. }
  954. /* Accept probe requests only for the control interface */
  955. if (!combined_interfaces && intf != control_interface)
  956. return -ENODEV;
  957. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  958. /* valid in this context */
  959. dev_dbg(&intf->dev, "The data interface isn't available\n");
  960. return -EBUSY;
  961. }
  962. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  963. return -EINVAL;
  964. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  965. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  966. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  967. /* workaround for switched endpoints */
  968. if (!usb_endpoint_dir_in(epread)) {
  969. /* descriptors are swapped */
  970. struct usb_endpoint_descriptor *t;
  971. dev_dbg(&intf->dev,
  972. "The data interface has switched endpoints\n");
  973. t = epread;
  974. epread = epwrite;
  975. epwrite = t;
  976. }
  977. made_compressed_probe:
  978. dbg("interfaces are valid");
  979. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  980. if (minor == ACM_TTY_MINORS) {
  981. dev_err(&intf->dev, "no more free acm devices\n");
  982. return -ENODEV;
  983. }
  984. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  985. if (acm == NULL) {
  986. dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
  987. goto alloc_fail;
  988. }
  989. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  990. readsize = le16_to_cpu(epread->wMaxPacketSize) *
  991. (quirks == SINGLE_RX_URB ? 1 : 2);
  992. acm->combined_interfaces = combined_interfaces;
  993. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
  994. acm->control = control_interface;
  995. acm->data = data_interface;
  996. acm->minor = minor;
  997. acm->dev = usb_dev;
  998. acm->ctrl_caps = ac_management_function;
  999. if (quirks & NO_CAP_LINE)
  1000. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1001. acm->ctrlsize = ctrlsize;
  1002. acm->readsize = readsize;
  1003. acm->rx_buflimit = num_rx_buf;
  1004. acm->urb_task.func = acm_rx_tasklet;
  1005. acm->urb_task.data = (unsigned long) acm;
  1006. INIT_WORK(&acm->work, acm_softint);
  1007. init_waitqueue_head(&acm->drain_wait);
  1008. spin_lock_init(&acm->throttle_lock);
  1009. spin_lock_init(&acm->write_lock);
  1010. spin_lock_init(&acm->read_lock);
  1011. mutex_init(&acm->mutex);
  1012. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  1013. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  1014. if (acm->is_int_ep)
  1015. acm->bInterval = epread->bInterval;
  1016. tty_port_init(&acm->port);
  1017. acm->port.ops = &acm_port_ops;
  1018. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1019. if (!buf) {
  1020. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  1021. goto alloc_fail2;
  1022. }
  1023. acm->ctrl_buffer = buf;
  1024. if (acm_write_buffers_alloc(acm) < 0) {
  1025. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  1026. goto alloc_fail4;
  1027. }
  1028. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1029. if (!acm->ctrlurb) {
  1030. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  1031. goto alloc_fail5;
  1032. }
  1033. for (i = 0; i < num_rx_buf; i++) {
  1034. struct acm_ru *rcv = &(acm->ru[i]);
  1035. rcv->urb = usb_alloc_urb(0, GFP_KERNEL);
  1036. if (rcv->urb == NULL) {
  1037. dev_dbg(&intf->dev,
  1038. "out of memory (read urbs usb_alloc_urb)\n");
  1039. goto alloc_fail6;
  1040. }
  1041. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1042. rcv->instance = acm;
  1043. }
  1044. for (i = 0; i < num_rx_buf; i++) {
  1045. struct acm_rb *rb = &(acm->rb[i]);
  1046. rb->base = usb_alloc_coherent(acm->dev, readsize,
  1047. GFP_KERNEL, &rb->dma);
  1048. if (!rb->base) {
  1049. dev_dbg(&intf->dev,
  1050. "out of memory (read bufs usb_alloc_coherent)\n");
  1051. goto alloc_fail7;
  1052. }
  1053. }
  1054. for (i = 0; i < ACM_NW; i++) {
  1055. struct acm_wb *snd = &(acm->wb[i]);
  1056. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1057. if (snd->urb == NULL) {
  1058. dev_dbg(&intf->dev,
  1059. "out of memory (write urbs usb_alloc_urb)");
  1060. goto alloc_fail8;
  1061. }
  1062. if (usb_endpoint_xfer_int(epwrite))
  1063. usb_fill_int_urb(snd->urb, usb_dev,
  1064. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1065. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1066. else
  1067. usb_fill_bulk_urb(snd->urb, usb_dev,
  1068. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  1069. NULL, acm->writesize, acm_write_bulk, snd);
  1070. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1071. snd->instance = acm;
  1072. }
  1073. usb_set_intfdata(intf, acm);
  1074. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1075. if (i < 0)
  1076. goto alloc_fail8;
  1077. if (cfd) { /* export the country data */
  1078. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1079. if (!acm->country_codes)
  1080. goto skip_countries;
  1081. acm->country_code_size = cfd->bLength - 4;
  1082. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1083. cfd->bLength - 4);
  1084. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1085. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1086. if (i < 0) {
  1087. kfree(acm->country_codes);
  1088. goto skip_countries;
  1089. }
  1090. i = device_create_file(&intf->dev,
  1091. &dev_attr_iCountryCodeRelDate);
  1092. if (i < 0) {
  1093. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1094. kfree(acm->country_codes);
  1095. goto skip_countries;
  1096. }
  1097. }
  1098. skip_countries:
  1099. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1100. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1101. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1102. /* works around buggy devices */
  1103. epctrl->bInterval ? epctrl->bInterval : 0xff);
  1104. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1105. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1106. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1107. acm_set_control(acm, acm->ctrlout);
  1108. acm->line.dwDTERate = cpu_to_le32(9600);
  1109. acm->line.bDataBits = 8;
  1110. acm_set_line(acm, &acm->line);
  1111. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1112. usb_set_intfdata(data_interface, acm);
  1113. usb_get_intf(control_interface);
  1114. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1115. acm_table[minor] = acm;
  1116. return 0;
  1117. alloc_fail8:
  1118. for (i = 0; i < ACM_NW; i++)
  1119. usb_free_urb(acm->wb[i].urb);
  1120. alloc_fail7:
  1121. acm_read_buffers_free(acm);
  1122. alloc_fail6:
  1123. for (i = 0; i < num_rx_buf; i++)
  1124. usb_free_urb(acm->ru[i].urb);
  1125. usb_free_urb(acm->ctrlurb);
  1126. alloc_fail5:
  1127. acm_write_buffers_free(acm);
  1128. alloc_fail4:
  1129. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1130. alloc_fail2:
  1131. kfree(acm);
  1132. alloc_fail:
  1133. return -ENOMEM;
  1134. }
  1135. static void stop_data_traffic(struct acm *acm)
  1136. {
  1137. int i;
  1138. dbg("Entering stop_data_traffic");
  1139. tasklet_disable(&acm->urb_task);
  1140. usb_kill_urb(acm->ctrlurb);
  1141. for (i = 0; i < ACM_NW; i++)
  1142. usb_kill_urb(acm->wb[i].urb);
  1143. for (i = 0; i < acm->rx_buflimit; i++)
  1144. usb_kill_urb(acm->ru[i].urb);
  1145. tasklet_enable(&acm->urb_task);
  1146. cancel_work_sync(&acm->work);
  1147. }
  1148. static void acm_disconnect(struct usb_interface *intf)
  1149. {
  1150. struct acm *acm = usb_get_intfdata(intf);
  1151. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1152. struct tty_struct *tty;
  1153. /* sibling interface is already cleaning up */
  1154. if (!acm)
  1155. return;
  1156. mutex_lock(&open_mutex);
  1157. if (acm->country_codes) {
  1158. device_remove_file(&acm->control->dev,
  1159. &dev_attr_wCountryCodes);
  1160. device_remove_file(&acm->control->dev,
  1161. &dev_attr_iCountryCodeRelDate);
  1162. }
  1163. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1164. acm->dev = NULL;
  1165. usb_set_intfdata(acm->control, NULL);
  1166. usb_set_intfdata(acm->data, NULL);
  1167. stop_data_traffic(acm);
  1168. acm_write_buffers_free(acm);
  1169. usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer,
  1170. acm->ctrl_dma);
  1171. acm_read_buffers_free(acm);
  1172. if (!acm->combined_interfaces)
  1173. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1174. acm->data : acm->control);
  1175. if (acm->port.count == 0) {
  1176. acm_tty_unregister(acm);
  1177. mutex_unlock(&open_mutex);
  1178. return;
  1179. }
  1180. mutex_unlock(&open_mutex);
  1181. tty = tty_port_tty_get(&acm->port);
  1182. if (tty) {
  1183. tty_hangup(tty);
  1184. tty_kref_put(tty);
  1185. }
  1186. }
  1187. #ifdef CONFIG_PM
  1188. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1189. {
  1190. struct acm *acm = usb_get_intfdata(intf);
  1191. int cnt;
  1192. if (message.event & PM_EVENT_AUTO) {
  1193. int b;
  1194. spin_lock_irq(&acm->read_lock);
  1195. spin_lock(&acm->write_lock);
  1196. b = acm->processing + acm->transmitting;
  1197. spin_unlock(&acm->write_lock);
  1198. spin_unlock_irq(&acm->read_lock);
  1199. if (b)
  1200. return -EBUSY;
  1201. }
  1202. spin_lock_irq(&acm->read_lock);
  1203. spin_lock(&acm->write_lock);
  1204. cnt = acm->susp_count++;
  1205. spin_unlock(&acm->write_lock);
  1206. spin_unlock_irq(&acm->read_lock);
  1207. if (cnt)
  1208. return 0;
  1209. /*
  1210. we treat opened interfaces differently,
  1211. we must guard against open
  1212. */
  1213. mutex_lock(&acm->mutex);
  1214. if (acm->port.count)
  1215. stop_data_traffic(acm);
  1216. mutex_unlock(&acm->mutex);
  1217. return 0;
  1218. }
  1219. static int acm_resume(struct usb_interface *intf)
  1220. {
  1221. struct acm *acm = usb_get_intfdata(intf);
  1222. struct acm_wb *wb;
  1223. int rv = 0;
  1224. int cnt;
  1225. spin_lock_irq(&acm->read_lock);
  1226. acm->susp_count -= 1;
  1227. cnt = acm->susp_count;
  1228. spin_unlock_irq(&acm->read_lock);
  1229. if (cnt)
  1230. return 0;
  1231. mutex_lock(&acm->mutex);
  1232. if (acm->port.count) {
  1233. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1234. spin_lock_irq(&acm->write_lock);
  1235. if (acm->delayed_wb) {
  1236. wb = acm->delayed_wb;
  1237. acm->delayed_wb = NULL;
  1238. spin_unlock_irq(&acm->write_lock);
  1239. acm_start_wb(acm, wb);
  1240. } else {
  1241. spin_unlock_irq(&acm->write_lock);
  1242. }
  1243. /*
  1244. * delayed error checking because we must
  1245. * do the write path at all cost
  1246. */
  1247. if (rv < 0)
  1248. goto err_out;
  1249. tasklet_schedule(&acm->urb_task);
  1250. }
  1251. err_out:
  1252. mutex_unlock(&acm->mutex);
  1253. return rv;
  1254. }
  1255. static int acm_reset_resume(struct usb_interface *intf)
  1256. {
  1257. struct acm *acm = usb_get_intfdata(intf);
  1258. struct tty_struct *tty;
  1259. mutex_lock(&acm->mutex);
  1260. if (acm->port.count) {
  1261. tty = tty_port_tty_get(&acm->port);
  1262. if (tty) {
  1263. tty_hangup(tty);
  1264. tty_kref_put(tty);
  1265. }
  1266. }
  1267. mutex_unlock(&acm->mutex);
  1268. return acm_resume(intf);
  1269. }
  1270. #endif /* CONFIG_PM */
  1271. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1272. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1273. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1274. USB_CDC_ACM_PROTO_VENDOR)
  1275. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1276. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1277. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1278. USB_CDC_ACM_PROTO_VENDOR)
  1279. /*
  1280. * USB driver structure.
  1281. */
  1282. static const struct usb_device_id acm_ids[] = {
  1283. /* quirky and broken devices */
  1284. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1285. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1286. },
  1287. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1288. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1289. },
  1290. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1291. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1292. },
  1293. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1294. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1295. },
  1296. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1297. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1298. },
  1299. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1300. .driver_info = SINGLE_RX_URB,
  1301. },
  1302. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1303. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1304. },
  1305. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1306. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1307. },
  1308. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1309. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1310. },
  1311. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1312. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1313. },
  1314. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1315. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1316. },
  1317. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1318. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1319. },
  1320. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1321. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1322. },
  1323. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1324. },
  1325. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1326. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1327. data interface instead of
  1328. communications interface.
  1329. Maybe we should define a new
  1330. quirk for this. */
  1331. },
  1332. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1333. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1334. },
  1335. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1336. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1337. },
  1338. /* Nokia S60 phones expose two ACM channels. The first is
  1339. * a modem and is picked up by the standard AT-command
  1340. * information below. The second is 'vendor-specific' but
  1341. * is treated as a serial device at the S60 end, so we want
  1342. * to expose it on Linux too. */
  1343. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1344. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1345. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1346. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1347. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1348. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1349. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1350. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1351. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1352. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1353. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1354. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1355. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1356. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1357. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1358. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1359. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1360. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1361. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1362. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1363. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1364. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1365. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1366. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1367. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1368. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1369. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1370. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1371. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1372. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1373. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1374. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1375. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1376. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1377. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1378. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1379. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1380. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1381. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1382. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1383. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1384. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1385. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1386. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1387. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1388. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1389. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1390. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1391. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1392. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1393. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1394. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1395. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1396. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1397. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1398. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1399. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1400. /* Support Lego NXT using pbLua firmware */
  1401. { USB_DEVICE(0x0694, 0xff00),
  1402. .driver_info = NOT_A_MODEM,
  1403. },
  1404. /* control interfaces without any protocol set */
  1405. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1406. USB_CDC_PROTO_NONE) },
  1407. /* control interfaces with various AT-command sets */
  1408. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1409. USB_CDC_ACM_PROTO_AT_V25TER) },
  1410. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1411. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1412. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1413. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1414. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1415. USB_CDC_ACM_PROTO_AT_GSM) },
  1416. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1417. USB_CDC_ACM_PROTO_AT_3G) },
  1418. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1419. USB_CDC_ACM_PROTO_AT_CDMA) },
  1420. { }
  1421. };
  1422. MODULE_DEVICE_TABLE(usb, acm_ids);
  1423. static struct usb_driver acm_driver = {
  1424. .name = "cdc_acm",
  1425. .probe = acm_probe,
  1426. .disconnect = acm_disconnect,
  1427. #ifdef CONFIG_PM
  1428. .suspend = acm_suspend,
  1429. .resume = acm_resume,
  1430. .reset_resume = acm_reset_resume,
  1431. #endif
  1432. .id_table = acm_ids,
  1433. #ifdef CONFIG_PM
  1434. .supports_autosuspend = 1,
  1435. #endif
  1436. };
  1437. /*
  1438. * TTY driver structures.
  1439. */
  1440. static const struct tty_operations acm_ops = {
  1441. .open = acm_tty_open,
  1442. .close = acm_tty_close,
  1443. .hangup = acm_tty_hangup,
  1444. .write = acm_tty_write,
  1445. .write_room = acm_tty_write_room,
  1446. .ioctl = acm_tty_ioctl,
  1447. .throttle = acm_tty_throttle,
  1448. .unthrottle = acm_tty_unthrottle,
  1449. .chars_in_buffer = acm_tty_chars_in_buffer,
  1450. .break_ctl = acm_tty_break_ctl,
  1451. .set_termios = acm_tty_set_termios,
  1452. .tiocmget = acm_tty_tiocmget,
  1453. .tiocmset = acm_tty_tiocmset,
  1454. };
  1455. /*
  1456. * Init / exit.
  1457. */
  1458. static int __init acm_init(void)
  1459. {
  1460. int retval;
  1461. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1462. if (!acm_tty_driver)
  1463. return -ENOMEM;
  1464. acm_tty_driver->owner = THIS_MODULE,
  1465. acm_tty_driver->driver_name = "acm",
  1466. acm_tty_driver->name = "ttyACM",
  1467. acm_tty_driver->major = ACM_TTY_MAJOR,
  1468. acm_tty_driver->minor_start = 0,
  1469. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1470. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1471. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1472. acm_tty_driver->init_termios = tty_std_termios;
  1473. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1474. HUPCL | CLOCAL;
  1475. tty_set_operations(acm_tty_driver, &acm_ops);
  1476. retval = tty_register_driver(acm_tty_driver);
  1477. if (retval) {
  1478. put_tty_driver(acm_tty_driver);
  1479. return retval;
  1480. }
  1481. retval = usb_register(&acm_driver);
  1482. if (retval) {
  1483. tty_unregister_driver(acm_tty_driver);
  1484. put_tty_driver(acm_tty_driver);
  1485. return retval;
  1486. }
  1487. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1488. DRIVER_DESC "\n");
  1489. return 0;
  1490. }
  1491. static void __exit acm_exit(void)
  1492. {
  1493. usb_deregister(&acm_driver);
  1494. tty_unregister_driver(acm_tty_driver);
  1495. put_tty_driver(acm_tty_driver);
  1496. }
  1497. module_init(acm_init);
  1498. module_exit(acm_exit);
  1499. MODULE_AUTHOR(DRIVER_AUTHOR);
  1500. MODULE_DESCRIPTION(DRIVER_DESC);
  1501. MODULE_LICENSE("GPL");
  1502. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);