cdc-acm.c 52 KB

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