cdc-acm.c 48 KB

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