cdc-acm.c 51 KB

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