cdc-acm.c 50 KB

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