cdc-acm.c 53 KB

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