cdc-acm.c 46 KB

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