hso.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  9. * <ajb@spheresystems.co.uk>
  10. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  11. * Copyright (C) 2008 Novell, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  25. * USA
  26. *
  27. *
  28. *****************************************************************************/
  29. /******************************************************************************
  30. *
  31. * Description of the device:
  32. *
  33. * Interface 0: Contains the IP network interface on the bulk end points.
  34. * The multiplexed serial ports are using the interrupt and
  35. * control endpoints.
  36. * Interrupt contains a bitmap telling which multiplexed
  37. * serialport needs servicing.
  38. *
  39. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  40. * port is opened, as this have a huge impact on the network port
  41. * throughput.
  42. *
  43. * Interface 2: Standard modem interface - circuit switched interface, this
  44. * can be used to make a standard ppp connection however it
  45. * should not be used in conjunction with the IP network interface
  46. * enabled for USB performance reasons i.e. if using this set
  47. * ideally disable_net=1.
  48. *
  49. *****************************************************************************/
  50. #include <linux/sched.h>
  51. #include <linux/slab.h>
  52. #include <linux/init.h>
  53. #include <linux/delay.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/module.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/usb.h>
  58. #include <linux/timer.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/kmod.h>
  63. #include <linux/rfkill.h>
  64. #include <linux/ip.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/usb/cdc.h>
  67. #include <net/arp.h>
  68. #include <asm/byteorder.h>
  69. #include <linux/serial_core.h>
  70. #include <linux/serial.h>
  71. #define DRIVER_VERSION "1.2"
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __attribute__((packed));
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct usb_device *usb;
  245. struct usb_interface *interface;
  246. struct device *dev;
  247. struct kref ref;
  248. struct mutex mutex;
  249. };
  250. /* Type of interface */
  251. #define HSO_INTF_MASK 0xFF00
  252. #define HSO_INTF_MUX 0x0100
  253. #define HSO_INTF_BULK 0x0200
  254. /* Type of port */
  255. #define HSO_PORT_MASK 0xFF
  256. #define HSO_PORT_NO_PORT 0x0
  257. #define HSO_PORT_CONTROL 0x1
  258. #define HSO_PORT_APP 0x2
  259. #define HSO_PORT_GPS 0x3
  260. #define HSO_PORT_PCSC 0x4
  261. #define HSO_PORT_APP2 0x5
  262. #define HSO_PORT_GPS_CONTROL 0x6
  263. #define HSO_PORT_MSD 0x7
  264. #define HSO_PORT_VOICE 0x8
  265. #define HSO_PORT_DIAG2 0x9
  266. #define HSO_PORT_DIAG 0x10
  267. #define HSO_PORT_MODEM 0x11
  268. #define HSO_PORT_NETWORK 0x12
  269. /* Additional device info */
  270. #define HSO_INFO_MASK 0xFF000000
  271. #define HSO_INFO_CRC_BUG 0x01000000
  272. /*****************************************************************************/
  273. /* Prototypes */
  274. /*****************************************************************************/
  275. /* Serial driver functions */
  276. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  277. unsigned int set, unsigned int clear);
  278. static void ctrl_callback(struct urb *urb);
  279. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  280. static void hso_kick_transmit(struct hso_serial *serial);
  281. /* Helper functions */
  282. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  283. struct usb_device *usb, gfp_t gfp);
  284. static void log_usb_status(int status, const char *function);
  285. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  286. int type, int dir);
  287. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  288. static void hso_free_interface(struct usb_interface *intf);
  289. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  290. static int hso_stop_serial_device(struct hso_device *hso_dev);
  291. static int hso_start_net_device(struct hso_device *hso_dev);
  292. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  293. static int hso_stop_net_device(struct hso_device *hso_dev);
  294. static void hso_serial_ref_free(struct kref *ref);
  295. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  296. static int hso_mux_serial_read(struct hso_serial *serial);
  297. static void async_get_intf(struct work_struct *data);
  298. static void async_put_intf(struct work_struct *data);
  299. static int hso_put_activity(struct hso_device *hso_dev);
  300. static int hso_get_activity(struct hso_device *hso_dev);
  301. static void tiocmget_intr_callback(struct urb *urb);
  302. /*****************************************************************************/
  303. /* Helping functions */
  304. /*****************************************************************************/
  305. /* #define DEBUG */
  306. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  307. {
  308. return hso_dev->port_data.dev_net;
  309. }
  310. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  311. {
  312. return hso_dev->port_data.dev_serial;
  313. }
  314. /* Debugging functions */
  315. #ifdef DEBUG
  316. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  317. unsigned int len)
  318. {
  319. static char name[255];
  320. sprintf(name, "hso[%d:%s]", line_count, func_name);
  321. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  322. }
  323. #define DUMP(buf_, len_) \
  324. dbg_dump(__LINE__, __func__, buf_, len_)
  325. #define DUMP1(buf_, len_) \
  326. do { \
  327. if (0x01 & debug) \
  328. DUMP(buf_, len_); \
  329. } while (0)
  330. #else
  331. #define DUMP(buf_, len_)
  332. #define DUMP1(buf_, len_)
  333. #endif
  334. /* module parameters */
  335. static int debug;
  336. static int tty_major;
  337. static int disable_net;
  338. /* driver info */
  339. static const char driver_name[] = "hso";
  340. static const char tty_filename[] = "ttyHS";
  341. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  342. /* the usb driver itself (registered in hso_init) */
  343. static struct usb_driver hso_driver;
  344. /* serial structures */
  345. static struct tty_driver *tty_drv;
  346. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  347. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  348. static spinlock_t serial_table_lock;
  349. static const s32 default_port_spec[] = {
  350. HSO_INTF_MUX | HSO_PORT_NETWORK,
  351. HSO_INTF_BULK | HSO_PORT_DIAG,
  352. HSO_INTF_BULK | HSO_PORT_MODEM,
  353. 0
  354. };
  355. static const s32 icon321_port_spec[] = {
  356. HSO_INTF_MUX | HSO_PORT_NETWORK,
  357. HSO_INTF_BULK | HSO_PORT_DIAG2,
  358. HSO_INTF_BULK | HSO_PORT_MODEM,
  359. HSO_INTF_BULK | HSO_PORT_DIAG,
  360. 0
  361. };
  362. #define default_port_device(vendor, product) \
  363. USB_DEVICE(vendor, product), \
  364. .driver_info = (kernel_ulong_t)default_port_spec
  365. #define icon321_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)icon321_port_spec
  368. /* list of devices we support */
  369. static const struct usb_device_id hso_ids[] = {
  370. {default_port_device(0x0af0, 0x6711)},
  371. {default_port_device(0x0af0, 0x6731)},
  372. {default_port_device(0x0af0, 0x6751)},
  373. {default_port_device(0x0af0, 0x6771)},
  374. {default_port_device(0x0af0, 0x6791)},
  375. {default_port_device(0x0af0, 0x6811)},
  376. {default_port_device(0x0af0, 0x6911)},
  377. {default_port_device(0x0af0, 0x6951)},
  378. {default_port_device(0x0af0, 0x6971)},
  379. {default_port_device(0x0af0, 0x7011)},
  380. {default_port_device(0x0af0, 0x7031)},
  381. {default_port_device(0x0af0, 0x7051)},
  382. {default_port_device(0x0af0, 0x7071)},
  383. {default_port_device(0x0af0, 0x7111)},
  384. {default_port_device(0x0af0, 0x7211)},
  385. {default_port_device(0x0af0, 0x7251)},
  386. {default_port_device(0x0af0, 0x7271)},
  387. {default_port_device(0x0af0, 0x7311)},
  388. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  389. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  390. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  391. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  392. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  393. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  395. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  396. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  397. {USB_DEVICE(0x0af0, 0x7701)},
  398. {USB_DEVICE(0x0af0, 0x7801)},
  399. {USB_DEVICE(0x0af0, 0x7901)},
  400. {USB_DEVICE(0x0af0, 0x7361)},
  401. {icon321_port_device(0x0af0, 0xd051)},
  402. {}
  403. };
  404. MODULE_DEVICE_TABLE(usb, hso_ids);
  405. /* Sysfs attribute */
  406. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  407. struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct hso_device *hso_dev = dev->driver_data;
  411. char *port_name;
  412. if (!hso_dev)
  413. return 0;
  414. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  415. case HSO_PORT_CONTROL:
  416. port_name = "Control";
  417. break;
  418. case HSO_PORT_APP:
  419. port_name = "Application";
  420. break;
  421. case HSO_PORT_APP2:
  422. port_name = "Application2";
  423. break;
  424. case HSO_PORT_GPS:
  425. port_name = "GPS";
  426. break;
  427. case HSO_PORT_GPS_CONTROL:
  428. port_name = "GPS Control";
  429. break;
  430. case HSO_PORT_PCSC:
  431. port_name = "PCSC";
  432. break;
  433. case HSO_PORT_DIAG:
  434. port_name = "Diagnostic";
  435. break;
  436. case HSO_PORT_DIAG2:
  437. port_name = "Diagnostic2";
  438. break;
  439. case HSO_PORT_MODEM:
  440. port_name = "Modem";
  441. break;
  442. case HSO_PORT_NETWORK:
  443. port_name = "Network";
  444. break;
  445. default:
  446. port_name = "Unknown";
  447. break;
  448. }
  449. return sprintf(buf, "%s\n", port_name);
  450. }
  451. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  452. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  453. {
  454. int idx;
  455. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  456. if (serial->rx_urb[idx] == urb)
  457. return idx;
  458. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  459. return -1;
  460. }
  461. /* converts mux value to a port spec value */
  462. static u32 hso_mux_to_port(int mux)
  463. {
  464. u32 result;
  465. switch (mux) {
  466. case 0x1:
  467. result = HSO_PORT_CONTROL;
  468. break;
  469. case 0x2:
  470. result = HSO_PORT_APP;
  471. break;
  472. case 0x4:
  473. result = HSO_PORT_PCSC;
  474. break;
  475. case 0x8:
  476. result = HSO_PORT_GPS;
  477. break;
  478. case 0x10:
  479. result = HSO_PORT_APP2;
  480. break;
  481. default:
  482. result = HSO_PORT_NO_PORT;
  483. }
  484. return result;
  485. }
  486. /* converts port spec value to a mux value */
  487. static u32 hso_port_to_mux(int port)
  488. {
  489. u32 result;
  490. switch (port & HSO_PORT_MASK) {
  491. case HSO_PORT_CONTROL:
  492. result = 0x0;
  493. break;
  494. case HSO_PORT_APP:
  495. result = 0x1;
  496. break;
  497. case HSO_PORT_PCSC:
  498. result = 0x2;
  499. break;
  500. case HSO_PORT_GPS:
  501. result = 0x3;
  502. break;
  503. case HSO_PORT_APP2:
  504. result = 0x4;
  505. break;
  506. default:
  507. result = 0x0;
  508. }
  509. return result;
  510. }
  511. static struct hso_serial *get_serial_by_shared_int_and_type(
  512. struct hso_shared_int *shared_int,
  513. int mux)
  514. {
  515. int i, port;
  516. port = hso_mux_to_port(mux);
  517. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  518. if (serial_table[i]
  519. && (dev2ser(serial_table[i])->shared_int == shared_int)
  520. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  521. return dev2ser(serial_table[i]);
  522. }
  523. }
  524. return NULL;
  525. }
  526. static struct hso_serial *get_serial_by_index(unsigned index)
  527. {
  528. struct hso_serial *serial = NULL;
  529. unsigned long flags;
  530. spin_lock_irqsave(&serial_table_lock, flags);
  531. if (serial_table[index])
  532. serial = dev2ser(serial_table[index]);
  533. spin_unlock_irqrestore(&serial_table_lock, flags);
  534. return serial;
  535. }
  536. static int get_free_serial_index(void)
  537. {
  538. int index;
  539. unsigned long flags;
  540. spin_lock_irqsave(&serial_table_lock, flags);
  541. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  542. if (serial_table[index] == NULL) {
  543. spin_unlock_irqrestore(&serial_table_lock, flags);
  544. return index;
  545. }
  546. }
  547. spin_unlock_irqrestore(&serial_table_lock, flags);
  548. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  549. return -1;
  550. }
  551. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  552. {
  553. unsigned long flags;
  554. spin_lock_irqsave(&serial_table_lock, flags);
  555. if (serial)
  556. serial_table[index] = serial->parent;
  557. else
  558. serial_table[index] = NULL;
  559. spin_unlock_irqrestore(&serial_table_lock, flags);
  560. }
  561. /* log a meaningful explanation of an USB status */
  562. static void log_usb_status(int status, const char *function)
  563. {
  564. char *explanation;
  565. switch (status) {
  566. case -ENODEV:
  567. explanation = "no device";
  568. break;
  569. case -ENOENT:
  570. explanation = "endpoint not enabled";
  571. break;
  572. case -EPIPE:
  573. explanation = "endpoint stalled";
  574. break;
  575. case -ENOSPC:
  576. explanation = "not enough bandwidth";
  577. break;
  578. case -ESHUTDOWN:
  579. explanation = "device disabled";
  580. break;
  581. case -EHOSTUNREACH:
  582. explanation = "device suspended";
  583. break;
  584. case -EINVAL:
  585. case -EAGAIN:
  586. case -EFBIG:
  587. case -EMSGSIZE:
  588. explanation = "internal error";
  589. break;
  590. default:
  591. explanation = "unknown status";
  592. break;
  593. }
  594. D1("%s: received USB status - %s (%d)", function, explanation, status);
  595. }
  596. /* Network interface functions */
  597. /* called when net interface is brought up by ifconfig */
  598. static int hso_net_open(struct net_device *net)
  599. {
  600. struct hso_net *odev = netdev_priv(net);
  601. unsigned long flags = 0;
  602. if (!odev) {
  603. dev_err(&net->dev, "No net device !\n");
  604. return -ENODEV;
  605. }
  606. odev->skb_tx_buf = NULL;
  607. /* setup environment */
  608. spin_lock_irqsave(&odev->net_lock, flags);
  609. odev->rx_parse_state = WAIT_IP;
  610. odev->rx_buf_size = 0;
  611. odev->rx_buf_missing = sizeof(struct iphdr);
  612. spin_unlock_irqrestore(&odev->net_lock, flags);
  613. /* We are up and running. */
  614. set_bit(HSO_NET_RUNNING, &odev->flags);
  615. hso_start_net_device(odev->parent);
  616. /* Tell the kernel we are ready to start receiving from it */
  617. netif_start_queue(net);
  618. return 0;
  619. }
  620. /* called when interface is brought down by ifconfig */
  621. static int hso_net_close(struct net_device *net)
  622. {
  623. struct hso_net *odev = netdev_priv(net);
  624. /* we don't need the queue anymore */
  625. netif_stop_queue(net);
  626. /* no longer running */
  627. clear_bit(HSO_NET_RUNNING, &odev->flags);
  628. hso_stop_net_device(odev->parent);
  629. /* done */
  630. return 0;
  631. }
  632. /* USB tells is xmit done, we should start the netqueue again */
  633. static void write_bulk_callback(struct urb *urb)
  634. {
  635. struct hso_net *odev = urb->context;
  636. int status = urb->status;
  637. /* Sanity check */
  638. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  639. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  640. return;
  641. }
  642. /* Do we still have a valid kernel network device? */
  643. if (!netif_device_present(odev->net)) {
  644. dev_err(&urb->dev->dev, "%s: net device not present\n",
  645. __func__);
  646. return;
  647. }
  648. /* log status, but don't act on it, we don't need to resubmit anything
  649. * anyhow */
  650. if (status)
  651. log_usb_status(status, __func__);
  652. hso_put_activity(odev->parent);
  653. /* Tell the network interface we are ready for another frame */
  654. netif_wake_queue(odev->net);
  655. }
  656. /* called by kernel when we need to transmit a packet */
  657. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  658. {
  659. struct hso_net *odev = netdev_priv(net);
  660. int result;
  661. /* Tell the kernel, "No more frames 'til we are done with this one." */
  662. netif_stop_queue(net);
  663. if (hso_get_activity(odev->parent) == -EAGAIN) {
  664. odev->skb_tx_buf = skb;
  665. return 0;
  666. }
  667. /* log if asked */
  668. DUMP1(skb->data, skb->len);
  669. /* Copy it from kernel memory to OUR memory */
  670. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  671. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  672. /* Fill in the URB for shipping it out. */
  673. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  674. odev->parent->usb,
  675. usb_sndbulkpipe(odev->parent->usb,
  676. odev->out_endp->
  677. bEndpointAddress & 0x7F),
  678. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  679. odev);
  680. /* Deal with the Zero Length packet problem, I hope */
  681. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  682. /* Send the URB on its merry way. */
  683. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  684. if (result) {
  685. dev_warn(&odev->parent->interface->dev,
  686. "failed mux_bulk_tx_urb %d", result);
  687. net->stats.tx_errors++;
  688. netif_start_queue(net);
  689. } else {
  690. net->stats.tx_packets++;
  691. net->stats.tx_bytes += skb->len;
  692. /* And tell the kernel when the last transmit started. */
  693. net->trans_start = jiffies;
  694. }
  695. dev_kfree_skb(skb);
  696. /* we're done */
  697. return result;
  698. }
  699. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  700. {
  701. struct hso_net *odev = netdev_priv(net);
  702. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  703. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  704. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  705. }
  706. static struct ethtool_ops ops = {
  707. .get_drvinfo = hso_get_drvinfo,
  708. .get_link = ethtool_op_get_link
  709. };
  710. /* called when a packet did not ack after watchdogtimeout */
  711. static void hso_net_tx_timeout(struct net_device *net)
  712. {
  713. struct hso_net *odev = netdev_priv(net);
  714. if (!odev)
  715. return;
  716. /* Tell syslog we are hosed. */
  717. dev_warn(&net->dev, "Tx timed out.\n");
  718. /* Tear the waiting frame off the list */
  719. if (odev->mux_bulk_tx_urb
  720. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  721. usb_unlink_urb(odev->mux_bulk_tx_urb);
  722. /* Update statistics */
  723. net->stats.tx_errors++;
  724. }
  725. /* make a real packet from the received USB buffer */
  726. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  727. unsigned int count, unsigned char is_eop)
  728. {
  729. unsigned short temp_bytes;
  730. unsigned short buffer_offset = 0;
  731. unsigned short frame_len;
  732. unsigned char *tmp_rx_buf;
  733. /* log if needed */
  734. D1("Rx %d bytes", count);
  735. DUMP(ip_pkt, min(128, (int)count));
  736. while (count) {
  737. switch (odev->rx_parse_state) {
  738. case WAIT_IP:
  739. /* waiting for IP header. */
  740. /* wanted bytes - size of ip header */
  741. temp_bytes =
  742. (count <
  743. odev->rx_buf_missing) ? count : odev->
  744. rx_buf_missing;
  745. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  746. odev->rx_buf_size, ip_pkt + buffer_offset,
  747. temp_bytes);
  748. odev->rx_buf_size += temp_bytes;
  749. buffer_offset += temp_bytes;
  750. odev->rx_buf_missing -= temp_bytes;
  751. count -= temp_bytes;
  752. if (!odev->rx_buf_missing) {
  753. /* header is complete allocate an sk_buffer and
  754. * continue to WAIT_DATA */
  755. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  756. if ((frame_len > DEFAULT_MRU) ||
  757. (frame_len < sizeof(struct iphdr))) {
  758. dev_err(&odev->net->dev,
  759. "Invalid frame (%d) length\n",
  760. frame_len);
  761. odev->rx_parse_state = WAIT_SYNC;
  762. continue;
  763. }
  764. /* Allocate an sk_buff */
  765. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  766. if (!odev->skb_rx_buf) {
  767. /* We got no receive buffer. */
  768. D1("could not allocate memory");
  769. odev->rx_parse_state = WAIT_SYNC;
  770. return;
  771. }
  772. /* Here's where it came from */
  773. odev->skb_rx_buf->dev = odev->net;
  774. /* Copy what we got so far. make room for iphdr
  775. * after tail. */
  776. tmp_rx_buf =
  777. skb_put(odev->skb_rx_buf,
  778. sizeof(struct iphdr));
  779. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  780. sizeof(struct iphdr));
  781. /* ETH_HLEN */
  782. odev->rx_buf_size = sizeof(struct iphdr);
  783. /* Filip actually use .tot_len */
  784. odev->rx_buf_missing =
  785. frame_len - sizeof(struct iphdr);
  786. odev->rx_parse_state = WAIT_DATA;
  787. }
  788. break;
  789. case WAIT_DATA:
  790. temp_bytes = (count < odev->rx_buf_missing)
  791. ? count : odev->rx_buf_missing;
  792. /* Copy the rest of the bytes that are left in the
  793. * buffer into the waiting sk_buf. */
  794. /* Make room for temp_bytes after tail. */
  795. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  796. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  797. odev->rx_buf_missing -= temp_bytes;
  798. count -= temp_bytes;
  799. buffer_offset += temp_bytes;
  800. odev->rx_buf_size += temp_bytes;
  801. if (!odev->rx_buf_missing) {
  802. /* Packet is complete. Inject into stack. */
  803. /* We have IP packet here */
  804. odev->skb_rx_buf->protocol =
  805. __constant_htons(ETH_P_IP);
  806. /* don't check it */
  807. odev->skb_rx_buf->ip_summed =
  808. CHECKSUM_UNNECESSARY;
  809. skb_reset_mac_header(odev->skb_rx_buf);
  810. /* Ship it off to the kernel */
  811. netif_rx(odev->skb_rx_buf);
  812. /* No longer our buffer. */
  813. odev->skb_rx_buf = NULL;
  814. /* update out statistics */
  815. odev->net->stats.rx_packets++;
  816. odev->net->stats.rx_bytes += odev->rx_buf_size;
  817. odev->rx_buf_size = 0;
  818. odev->rx_buf_missing = sizeof(struct iphdr);
  819. odev->rx_parse_state = WAIT_IP;
  820. }
  821. break;
  822. case WAIT_SYNC:
  823. D1(" W_S");
  824. count = 0;
  825. break;
  826. default:
  827. D1(" ");
  828. count--;
  829. break;
  830. }
  831. }
  832. /* Recovery mechanism for WAIT_SYNC state. */
  833. if (is_eop) {
  834. if (odev->rx_parse_state == WAIT_SYNC) {
  835. odev->rx_parse_state = WAIT_IP;
  836. odev->rx_buf_size = 0;
  837. odev->rx_buf_missing = sizeof(struct iphdr);
  838. }
  839. }
  840. }
  841. /* Moving data from usb to kernel (in interrupt state) */
  842. static void read_bulk_callback(struct urb *urb)
  843. {
  844. struct hso_net *odev = urb->context;
  845. struct net_device *net;
  846. int result;
  847. int status = urb->status;
  848. /* is al ok? (Filip: Who's Al ?) */
  849. if (status) {
  850. log_usb_status(status, __func__);
  851. return;
  852. }
  853. /* Sanity check */
  854. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  855. D1("BULK IN callback but driver is not active!");
  856. return;
  857. }
  858. usb_mark_last_busy(urb->dev);
  859. net = odev->net;
  860. if (!netif_device_present(net)) {
  861. /* Somebody killed our network interface... */
  862. return;
  863. }
  864. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  865. u32 rest;
  866. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  867. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  868. if (((rest == 5) || (rest == 6))
  869. && !memcmp(((u8 *) urb->transfer_buffer) +
  870. urb->actual_length - 4, crc_check, 4)) {
  871. urb->actual_length -= 4;
  872. }
  873. }
  874. /* do we even have a packet? */
  875. if (urb->actual_length) {
  876. /* Handle the IP stream, add header and push it onto network
  877. * stack if the packet is complete. */
  878. spin_lock(&odev->net_lock);
  879. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  880. (urb->transfer_buffer_length >
  881. urb->actual_length) ? 1 : 0);
  882. spin_unlock(&odev->net_lock);
  883. }
  884. /* We are done with this URB, resubmit it. Prep the USB to wait for
  885. * another frame. Reuse same as received. */
  886. usb_fill_bulk_urb(urb,
  887. odev->parent->usb,
  888. usb_rcvbulkpipe(odev->parent->usb,
  889. odev->in_endp->
  890. bEndpointAddress & 0x7F),
  891. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  892. read_bulk_callback, odev);
  893. /* Give this to the USB subsystem so it can tell us when more data
  894. * arrives. */
  895. result = usb_submit_urb(urb, GFP_ATOMIC);
  896. if (result)
  897. dev_warn(&odev->parent->interface->dev,
  898. "%s failed submit mux_bulk_rx_urb %d", __func__,
  899. result);
  900. }
  901. /* Serial driver functions */
  902. static void hso_init_termios(struct ktermios *termios)
  903. {
  904. /*
  905. * The default requirements for this device are:
  906. */
  907. termios->c_iflag &=
  908. ~(IGNBRK /* disable ignore break */
  909. | BRKINT /* disable break causes interrupt */
  910. | PARMRK /* disable mark parity errors */
  911. | ISTRIP /* disable clear high bit of input characters */
  912. | INLCR /* disable translate NL to CR */
  913. | IGNCR /* disable ignore CR */
  914. | ICRNL /* disable translate CR to NL */
  915. | IXON); /* disable enable XON/XOFF flow control */
  916. /* disable postprocess output characters */
  917. termios->c_oflag &= ~OPOST;
  918. termios->c_lflag &=
  919. ~(ECHO /* disable echo input characters */
  920. | ECHONL /* disable echo new line */
  921. | ICANON /* disable erase, kill, werase, and rprnt
  922. special characters */
  923. | ISIG /* disable interrupt, quit, and suspend special
  924. characters */
  925. | IEXTEN); /* disable non-POSIX special characters */
  926. termios->c_cflag &=
  927. ~(CSIZE /* no size */
  928. | PARENB /* disable parity bit */
  929. | CBAUD /* clear current baud rate */
  930. | CBAUDEX); /* clear current buad rate */
  931. termios->c_cflag |= CS8; /* character size 8 bits */
  932. /* baud rate 115200 */
  933. tty_termios_encode_baud_rate(termios, 115200, 115200);
  934. }
  935. static void _hso_serial_set_termios(struct tty_struct *tty,
  936. struct ktermios *old)
  937. {
  938. struct hso_serial *serial = get_serial_by_tty(tty);
  939. struct ktermios *termios;
  940. if (!serial) {
  941. printk(KERN_ERR "%s: no tty structures", __func__);
  942. return;
  943. }
  944. D4("port %d", serial->minor);
  945. /*
  946. * Fix up unsupported bits
  947. */
  948. termios = tty->termios;
  949. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  950. termios->c_cflag &=
  951. ~(CSIZE /* no size */
  952. | PARENB /* disable parity bit */
  953. | CBAUD /* clear current baud rate */
  954. | CBAUDEX); /* clear current buad rate */
  955. termios->c_cflag |= CS8; /* character size 8 bits */
  956. /* baud rate 115200 */
  957. tty_encode_baud_rate(tty, 115200, 115200);
  958. }
  959. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  960. {
  961. int result;
  962. #ifdef CONFIG_HSO_AUTOPM
  963. usb_mark_last_busy(urb->dev);
  964. #endif
  965. /* We are done with this URB, resubmit it. Prep the USB to wait for
  966. * another frame */
  967. usb_fill_bulk_urb(urb, serial->parent->usb,
  968. usb_rcvbulkpipe(serial->parent->usb,
  969. serial->in_endp->
  970. bEndpointAddress & 0x7F),
  971. urb->transfer_buffer, serial->rx_data_length,
  972. hso_std_serial_read_bulk_callback, serial);
  973. /* Give this to the USB subsystem so it can tell us when more data
  974. * arrives. */
  975. result = usb_submit_urb(urb, GFP_ATOMIC);
  976. if (result) {
  977. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  978. __func__, result);
  979. }
  980. }
  981. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  982. {
  983. int count;
  984. struct urb *curr_urb;
  985. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  986. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  987. count = put_rxbuf_data(curr_urb, serial);
  988. if (count == -1)
  989. return;
  990. if (count == 0) {
  991. serial->curr_rx_urb_idx++;
  992. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  993. serial->curr_rx_urb_idx = 0;
  994. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  995. }
  996. }
  997. }
  998. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  999. {
  1000. int count = 0;
  1001. struct urb *urb;
  1002. urb = serial->rx_urb[0];
  1003. if (serial->open_count > 0) {
  1004. count = put_rxbuf_data(urb, serial);
  1005. if (count == -1)
  1006. return;
  1007. }
  1008. /* Re issue a read as long as we receive data. */
  1009. if (count == 0 && ((urb->actual_length != 0) ||
  1010. (serial->rx_state == RX_PENDING))) {
  1011. serial->rx_state = RX_SENT;
  1012. hso_mux_serial_read(serial);
  1013. } else
  1014. serial->rx_state = RX_IDLE;
  1015. }
  1016. /* read callback for Diag and CS port */
  1017. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1018. {
  1019. struct hso_serial *serial = urb->context;
  1020. int status = urb->status;
  1021. /* sanity check */
  1022. if (!serial) {
  1023. D1("serial == NULL");
  1024. return;
  1025. } else if (status) {
  1026. log_usb_status(status, __func__);
  1027. return;
  1028. }
  1029. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1030. D1("Actual length = %d\n", urb->actual_length);
  1031. DUMP1(urb->transfer_buffer, urb->actual_length);
  1032. /* Anyone listening? */
  1033. if (serial->open_count == 0)
  1034. return;
  1035. if (status == 0) {
  1036. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1037. u32 rest;
  1038. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1039. rest =
  1040. urb->actual_length %
  1041. serial->in_endp->wMaxPacketSize;
  1042. if (((rest == 5) || (rest == 6))
  1043. && !memcmp(((u8 *) urb->transfer_buffer) +
  1044. urb->actual_length - 4, crc_check, 4)) {
  1045. urb->actual_length -= 4;
  1046. }
  1047. }
  1048. /* Valid data, handle RX data */
  1049. spin_lock(&serial->serial_lock);
  1050. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1051. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1052. spin_unlock(&serial->serial_lock);
  1053. } else if (status == -ENOENT || status == -ECONNRESET) {
  1054. /* Unlinked - check for throttled port. */
  1055. D2("Port %d, successfully unlinked urb", serial->minor);
  1056. spin_lock(&serial->serial_lock);
  1057. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1058. hso_resubmit_rx_bulk_urb(serial, urb);
  1059. spin_unlock(&serial->serial_lock);
  1060. } else {
  1061. D2("Port %d, status = %d for read urb", serial->minor, status);
  1062. return;
  1063. }
  1064. }
  1065. /*
  1066. * This needs to be a tasklet otherwise we will
  1067. * end up recursively calling this function.
  1068. */
  1069. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1070. {
  1071. unsigned long flags;
  1072. spin_lock_irqsave(&serial->serial_lock, flags);
  1073. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1074. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1075. else
  1076. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1077. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1078. }
  1079. static void hso_unthrottle(struct tty_struct *tty)
  1080. {
  1081. struct hso_serial *serial = get_serial_by_tty(tty);
  1082. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1083. }
  1084. void hso_unthrottle_workfunc(struct work_struct *work)
  1085. {
  1086. struct hso_serial *serial =
  1087. container_of(work, struct hso_serial,
  1088. retry_unthrottle_workqueue);
  1089. hso_unthrottle_tasklet(serial);
  1090. }
  1091. /* open the requested serial port */
  1092. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1093. {
  1094. struct hso_serial *serial = get_serial_by_index(tty->index);
  1095. int result;
  1096. /* sanity check */
  1097. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1098. WARN_ON(1);
  1099. tty->driver_data = NULL;
  1100. D1("Failed to open port");
  1101. return -ENODEV;
  1102. }
  1103. mutex_lock(&serial->parent->mutex);
  1104. result = usb_autopm_get_interface(serial->parent->interface);
  1105. if (result < 0)
  1106. goto err_out;
  1107. D1("Opening %d", serial->minor);
  1108. kref_get(&serial->parent->ref);
  1109. /* setup */
  1110. spin_lock_irq(&serial->serial_lock);
  1111. tty->driver_data = serial;
  1112. tty_kref_put(serial->tty);
  1113. serial->tty = tty_kref_get(tty);
  1114. spin_unlock_irq(&serial->serial_lock);
  1115. /* check for port already opened, if not set the termios */
  1116. serial->open_count++;
  1117. if (serial->open_count == 1) {
  1118. tty->low_latency = 1;
  1119. serial->rx_state = RX_IDLE;
  1120. /* Force default termio settings */
  1121. _hso_serial_set_termios(tty, NULL);
  1122. tasklet_init(&serial->unthrottle_tasklet,
  1123. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1124. (unsigned long)serial);
  1125. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1126. hso_unthrottle_workfunc);
  1127. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1128. if (result) {
  1129. hso_stop_serial_device(serial->parent);
  1130. serial->open_count--;
  1131. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1132. }
  1133. } else {
  1134. D1("Port was already open");
  1135. }
  1136. usb_autopm_put_interface(serial->parent->interface);
  1137. /* done */
  1138. if (result)
  1139. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1140. err_out:
  1141. mutex_unlock(&serial->parent->mutex);
  1142. return result;
  1143. }
  1144. /* close the requested serial port */
  1145. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1146. {
  1147. struct hso_serial *serial = tty->driver_data;
  1148. u8 usb_gone;
  1149. D1("Closing serial port");
  1150. /* Open failed, no close cleanup required */
  1151. if (serial == NULL)
  1152. return;
  1153. mutex_lock(&serial->parent->mutex);
  1154. usb_gone = serial->parent->usb_gone;
  1155. if (!usb_gone)
  1156. usb_autopm_get_interface(serial->parent->interface);
  1157. /* reset the rts and dtr */
  1158. /* do the actual close */
  1159. serial->open_count--;
  1160. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1161. if (serial->open_count <= 0) {
  1162. serial->open_count = 0;
  1163. spin_lock_irq(&serial->serial_lock);
  1164. if (serial->tty == tty) {
  1165. serial->tty->driver_data = NULL;
  1166. serial->tty = NULL;
  1167. tty_kref_put(tty);
  1168. }
  1169. spin_unlock_irq(&serial->serial_lock);
  1170. if (!usb_gone)
  1171. hso_stop_serial_device(serial->parent);
  1172. tasklet_kill(&serial->unthrottle_tasklet);
  1173. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1174. }
  1175. if (!usb_gone)
  1176. usb_autopm_put_interface(serial->parent->interface);
  1177. mutex_unlock(&serial->parent->mutex);
  1178. }
  1179. /* close the requested serial port */
  1180. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1181. int count)
  1182. {
  1183. struct hso_serial *serial = get_serial_by_tty(tty);
  1184. int space, tx_bytes;
  1185. unsigned long flags;
  1186. /* sanity check */
  1187. if (serial == NULL) {
  1188. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1189. return -ENODEV;
  1190. }
  1191. spin_lock_irqsave(&serial->serial_lock, flags);
  1192. space = serial->tx_data_length - serial->tx_buffer_count;
  1193. tx_bytes = (count < space) ? count : space;
  1194. if (!tx_bytes)
  1195. goto out;
  1196. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1197. serial->tx_buffer_count += tx_bytes;
  1198. out:
  1199. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1200. hso_kick_transmit(serial);
  1201. /* done */
  1202. return tx_bytes;
  1203. }
  1204. /* how much room is there for writing */
  1205. static int hso_serial_write_room(struct tty_struct *tty)
  1206. {
  1207. struct hso_serial *serial = get_serial_by_tty(tty);
  1208. int room;
  1209. unsigned long flags;
  1210. spin_lock_irqsave(&serial->serial_lock, flags);
  1211. room = serial->tx_data_length - serial->tx_buffer_count;
  1212. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1213. /* return free room */
  1214. return room;
  1215. }
  1216. /* setup the term */
  1217. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1218. {
  1219. struct hso_serial *serial = get_serial_by_tty(tty);
  1220. unsigned long flags;
  1221. if (old)
  1222. D5("Termios called with: cflags new[%d] - old[%d]",
  1223. tty->termios->c_cflag, old->c_cflag);
  1224. /* the actual setup */
  1225. spin_lock_irqsave(&serial->serial_lock, flags);
  1226. if (serial->open_count)
  1227. _hso_serial_set_termios(tty, old);
  1228. else
  1229. tty->termios = old;
  1230. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1231. /* done */
  1232. return;
  1233. }
  1234. /* how many characters in the buffer */
  1235. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1236. {
  1237. struct hso_serial *serial = get_serial_by_tty(tty);
  1238. int chars;
  1239. unsigned long flags;
  1240. /* sanity check */
  1241. if (serial == NULL)
  1242. return 0;
  1243. spin_lock_irqsave(&serial->serial_lock, flags);
  1244. chars = serial->tx_buffer_count;
  1245. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1246. return chars;
  1247. }
  1248. int tiocmget_submit_urb(struct hso_serial *serial,
  1249. struct hso_tiocmget *tiocmget,
  1250. struct usb_device *usb)
  1251. {
  1252. int result;
  1253. if (serial->parent->usb_gone)
  1254. return -ENODEV;
  1255. usb_fill_int_urb(tiocmget->urb, usb,
  1256. usb_rcvintpipe(usb,
  1257. tiocmget->endp->
  1258. bEndpointAddress & 0x7F),
  1259. &tiocmget->serial_state_notification,
  1260. sizeof(struct hso_serial_state_notification),
  1261. tiocmget_intr_callback, serial,
  1262. tiocmget->endp->bInterval);
  1263. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1264. if (result) {
  1265. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1266. result);
  1267. }
  1268. return result;
  1269. }
  1270. static void tiocmget_intr_callback(struct urb *urb)
  1271. {
  1272. struct hso_serial *serial = urb->context;
  1273. struct hso_tiocmget *tiocmget;
  1274. int status = urb->status;
  1275. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1276. struct uart_icount *icount;
  1277. struct hso_serial_state_notification *serial_state_notification;
  1278. struct usb_device *usb;
  1279. /* Sanity checks */
  1280. if (!serial)
  1281. return;
  1282. if (status) {
  1283. log_usb_status(status, __func__);
  1284. return;
  1285. }
  1286. tiocmget = serial->tiocmget;
  1287. if (!tiocmget)
  1288. return;
  1289. usb = serial->parent->usb;
  1290. serial_state_notification = &tiocmget->serial_state_notification;
  1291. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1292. serial_state_notification->bNotification != B_NOTIFICATION ||
  1293. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1294. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1295. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1296. dev_warn(&usb->dev,
  1297. "hso received invalid serial state notification\n");
  1298. DUMP(serial_state_notification,
  1299. sizeof(hso_serial_state_notifation))
  1300. } else {
  1301. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1302. UART_state_bitmap);
  1303. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1304. icount = &tiocmget->icount;
  1305. spin_lock(&serial->serial_lock);
  1306. if ((UART_state_bitmap & B_OVERRUN) !=
  1307. (prev_UART_state_bitmap & B_OVERRUN))
  1308. icount->parity++;
  1309. if ((UART_state_bitmap & B_PARITY) !=
  1310. (prev_UART_state_bitmap & B_PARITY))
  1311. icount->parity++;
  1312. if ((UART_state_bitmap & B_FRAMING) !=
  1313. (prev_UART_state_bitmap & B_FRAMING))
  1314. icount->frame++;
  1315. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1316. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1317. icount->rng++;
  1318. if ((UART_state_bitmap & B_BREAK) !=
  1319. (prev_UART_state_bitmap & B_BREAK))
  1320. icount->brk++;
  1321. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1322. (prev_UART_state_bitmap & B_TX_CARRIER))
  1323. icount->dsr++;
  1324. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1325. (prev_UART_state_bitmap & B_RX_CARRIER))
  1326. icount->dcd++;
  1327. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1328. spin_unlock(&serial->serial_lock);
  1329. tiocmget->intr_completed = 1;
  1330. wake_up_interruptible(&tiocmget->waitq);
  1331. }
  1332. memset(serial_state_notification, 0,
  1333. sizeof(struct hso_serial_state_notification));
  1334. tiocmget_submit_urb(serial,
  1335. tiocmget,
  1336. serial->parent->usb);
  1337. }
  1338. /*
  1339. * next few functions largely stolen from drivers/serial/serial_core.c
  1340. */
  1341. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1342. * - mask passed in arg for lines of interest
  1343. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1344. * Caller should use TIOCGICOUNT to see which one it was
  1345. */
  1346. static int
  1347. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1348. {
  1349. DECLARE_WAITQUEUE(wait, current);
  1350. struct uart_icount cprev, cnow;
  1351. struct hso_tiocmget *tiocmget;
  1352. int ret;
  1353. tiocmget = serial->tiocmget;
  1354. if (!tiocmget)
  1355. return -ENOENT;
  1356. /*
  1357. * note the counters on entry
  1358. */
  1359. spin_lock_irq(&serial->serial_lock);
  1360. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1361. spin_unlock_irq(&serial->serial_lock);
  1362. add_wait_queue(&tiocmget->waitq, &wait);
  1363. for (;;) {
  1364. spin_lock_irq(&serial->serial_lock);
  1365. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1366. spin_unlock_irq(&serial->serial_lock);
  1367. set_current_state(TASK_INTERRUPTIBLE);
  1368. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1369. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1370. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1371. ret = 0;
  1372. break;
  1373. }
  1374. schedule();
  1375. /* see if a signal did it */
  1376. if (signal_pending(current)) {
  1377. ret = -ERESTARTSYS;
  1378. break;
  1379. }
  1380. cprev = cnow;
  1381. }
  1382. current->state = TASK_RUNNING;
  1383. remove_wait_queue(&tiocmget->waitq, &wait);
  1384. return ret;
  1385. }
  1386. /*
  1387. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1388. * Return: write counters to the user passed counter struct
  1389. * NB: both 1->0 and 0->1 transitions are counted except for
  1390. * RI where only 0->1 is counted.
  1391. */
  1392. static int hso_get_count(struct hso_serial *serial,
  1393. struct serial_icounter_struct __user *icnt)
  1394. {
  1395. struct serial_icounter_struct icount;
  1396. struct uart_icount cnow;
  1397. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1398. if (!tiocmget)
  1399. return -ENOENT;
  1400. spin_lock_irq(&serial->serial_lock);
  1401. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1402. spin_unlock_irq(&serial->serial_lock);
  1403. icount.cts = cnow.cts;
  1404. icount.dsr = cnow.dsr;
  1405. icount.rng = cnow.rng;
  1406. icount.dcd = cnow.dcd;
  1407. icount.rx = cnow.rx;
  1408. icount.tx = cnow.tx;
  1409. icount.frame = cnow.frame;
  1410. icount.overrun = cnow.overrun;
  1411. icount.parity = cnow.parity;
  1412. icount.brk = cnow.brk;
  1413. icount.buf_overrun = cnow.buf_overrun;
  1414. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1415. }
  1416. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1417. {
  1418. int retval;
  1419. struct hso_serial *serial = get_serial_by_tty(tty);
  1420. struct hso_tiocmget *tiocmget;
  1421. u16 UART_state_bitmap;
  1422. /* sanity check */
  1423. if (!serial) {
  1424. D1("no tty structures");
  1425. return -EINVAL;
  1426. }
  1427. spin_lock_irq(&serial->serial_lock);
  1428. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1429. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1430. tiocmget = serial->tiocmget;
  1431. if (tiocmget) {
  1432. UART_state_bitmap = le16_to_cpu(
  1433. tiocmget->prev_UART_state_bitmap);
  1434. if (UART_state_bitmap & B_RING_SIGNAL)
  1435. retval |= TIOCM_RNG;
  1436. if (UART_state_bitmap & B_RX_CARRIER)
  1437. retval |= TIOCM_CD;
  1438. if (UART_state_bitmap & B_TX_CARRIER)
  1439. retval |= TIOCM_DSR;
  1440. }
  1441. spin_unlock_irq(&serial->serial_lock);
  1442. return retval;
  1443. }
  1444. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1445. unsigned int set, unsigned int clear)
  1446. {
  1447. int val = 0;
  1448. unsigned long flags;
  1449. int if_num;
  1450. struct hso_serial *serial = get_serial_by_tty(tty);
  1451. /* sanity check */
  1452. if (!serial) {
  1453. D1("no tty structures");
  1454. return -EINVAL;
  1455. }
  1456. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1457. spin_lock_irqsave(&serial->serial_lock, flags);
  1458. if (set & TIOCM_RTS)
  1459. serial->rts_state = 1;
  1460. if (set & TIOCM_DTR)
  1461. serial->dtr_state = 1;
  1462. if (clear & TIOCM_RTS)
  1463. serial->rts_state = 0;
  1464. if (clear & TIOCM_DTR)
  1465. serial->dtr_state = 0;
  1466. if (serial->dtr_state)
  1467. val |= 0x01;
  1468. if (serial->rts_state)
  1469. val |= 0x02;
  1470. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1471. return usb_control_msg(serial->parent->usb,
  1472. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1473. 0x21, val, if_num, NULL, 0,
  1474. USB_CTRL_SET_TIMEOUT);
  1475. }
  1476. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1477. unsigned int cmd, unsigned long arg)
  1478. {
  1479. struct hso_serial *serial = get_serial_by_tty(tty);
  1480. void __user *uarg = (void __user *)arg;
  1481. int ret = 0;
  1482. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1483. if (!serial)
  1484. return -ENODEV;
  1485. switch (cmd) {
  1486. case TIOCMIWAIT:
  1487. ret = hso_wait_modem_status(serial, arg);
  1488. break;
  1489. case TIOCGICOUNT:
  1490. ret = hso_get_count(serial, uarg);
  1491. break;
  1492. default:
  1493. ret = -ENOIOCTLCMD;
  1494. break;
  1495. }
  1496. return ret;
  1497. }
  1498. /* starts a transmit */
  1499. static void hso_kick_transmit(struct hso_serial *serial)
  1500. {
  1501. u8 *temp;
  1502. unsigned long flags;
  1503. int res;
  1504. spin_lock_irqsave(&serial->serial_lock, flags);
  1505. if (!serial->tx_buffer_count)
  1506. goto out;
  1507. if (serial->tx_urb_used)
  1508. goto out;
  1509. /* Wakeup USB interface if necessary */
  1510. if (hso_get_activity(serial->parent) == -EAGAIN)
  1511. goto out;
  1512. /* Switch pointers around to avoid memcpy */
  1513. temp = serial->tx_buffer;
  1514. serial->tx_buffer = serial->tx_data;
  1515. serial->tx_data = temp;
  1516. serial->tx_data_count = serial->tx_buffer_count;
  1517. serial->tx_buffer_count = 0;
  1518. /* If temp is set, it means we switched buffers */
  1519. if (temp && serial->write_data) {
  1520. res = serial->write_data(serial);
  1521. if (res >= 0)
  1522. serial->tx_urb_used = 1;
  1523. }
  1524. out:
  1525. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1526. }
  1527. /* make a request (for reading and writing data to muxed serial port) */
  1528. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1529. struct urb *ctrl_urb,
  1530. struct usb_ctrlrequest *ctrl_req,
  1531. u8 *ctrl_urb_data, u32 size)
  1532. {
  1533. int result;
  1534. int pipe;
  1535. /* Sanity check */
  1536. if (!serial || !ctrl_urb || !ctrl_req) {
  1537. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1538. return -EINVAL;
  1539. }
  1540. /* initialize */
  1541. ctrl_req->wValue = 0;
  1542. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1543. ctrl_req->wLength = cpu_to_le16(size);
  1544. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1545. /* Reading command */
  1546. ctrl_req->bRequestType = USB_DIR_IN |
  1547. USB_TYPE_OPTION_VENDOR |
  1548. USB_RECIP_INTERFACE;
  1549. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1550. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1551. } else {
  1552. /* Writing command */
  1553. ctrl_req->bRequestType = USB_DIR_OUT |
  1554. USB_TYPE_OPTION_VENDOR |
  1555. USB_RECIP_INTERFACE;
  1556. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1557. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1558. }
  1559. /* syslog */
  1560. D2("%s command (%02x) len: %d, port: %d",
  1561. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1562. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1563. /* Load ctrl urb */
  1564. ctrl_urb->transfer_flags = 0;
  1565. usb_fill_control_urb(ctrl_urb,
  1566. serial->parent->usb,
  1567. pipe,
  1568. (u8 *) ctrl_req,
  1569. ctrl_urb_data, size, ctrl_callback, serial);
  1570. /* Send it on merry way */
  1571. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1572. if (result) {
  1573. dev_err(&ctrl_urb->dev->dev,
  1574. "%s failed submit ctrl_urb %d type %d", __func__,
  1575. result, type);
  1576. return result;
  1577. }
  1578. /* done */
  1579. return size;
  1580. }
  1581. /* called by intr_callback when read occurs */
  1582. static int hso_mux_serial_read(struct hso_serial *serial)
  1583. {
  1584. if (!serial)
  1585. return -EINVAL;
  1586. /* clean data */
  1587. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1588. /* make the request */
  1589. if (serial->num_rx_urbs != 1) {
  1590. dev_err(&serial->parent->interface->dev,
  1591. "ERROR: mux'd reads with multiple buffers "
  1592. "not possible\n");
  1593. return 0;
  1594. }
  1595. return mux_device_request(serial,
  1596. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1597. serial->parent->port_spec & HSO_PORT_MASK,
  1598. serial->rx_urb[0],
  1599. &serial->ctrl_req_rx,
  1600. serial->rx_data[0], serial->rx_data_length);
  1601. }
  1602. /* used for muxed serial port callback (muxed serial read) */
  1603. static void intr_callback(struct urb *urb)
  1604. {
  1605. struct hso_shared_int *shared_int = urb->context;
  1606. struct hso_serial *serial;
  1607. unsigned char *port_req;
  1608. int status = urb->status;
  1609. int i;
  1610. usb_mark_last_busy(urb->dev);
  1611. /* sanity check */
  1612. if (!shared_int)
  1613. return;
  1614. /* status check */
  1615. if (status) {
  1616. log_usb_status(status, __func__);
  1617. return;
  1618. }
  1619. D4("\n--- Got intr callback 0x%02X ---", status);
  1620. /* what request? */
  1621. port_req = urb->transfer_buffer;
  1622. D4(" port_req = 0x%.2X\n", *port_req);
  1623. /* loop over all muxed ports to find the one sending this */
  1624. for (i = 0; i < 8; i++) {
  1625. /* max 8 channels on MUX */
  1626. if (*port_req & (1 << i)) {
  1627. serial = get_serial_by_shared_int_and_type(shared_int,
  1628. (1 << i));
  1629. if (serial != NULL) {
  1630. D1("Pending read interrupt on port %d\n", i);
  1631. spin_lock(&serial->serial_lock);
  1632. if (serial->rx_state == RX_IDLE) {
  1633. /* Setup and send a ctrl req read on
  1634. * port i */
  1635. if (!serial->rx_urb_filled[0]) {
  1636. serial->rx_state = RX_SENT;
  1637. hso_mux_serial_read(serial);
  1638. } else
  1639. serial->rx_state = RX_PENDING;
  1640. } else {
  1641. D1("Already pending a read on "
  1642. "port %d\n", i);
  1643. }
  1644. spin_unlock(&serial->serial_lock);
  1645. }
  1646. }
  1647. }
  1648. /* Resubmit interrupt urb */
  1649. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1650. }
  1651. /* called for writing to muxed serial port */
  1652. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1653. {
  1654. if (NULL == serial)
  1655. return -EINVAL;
  1656. return mux_device_request(serial,
  1657. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1658. serial->parent->port_spec & HSO_PORT_MASK,
  1659. serial->tx_urb,
  1660. &serial->ctrl_req_tx,
  1661. serial->tx_data, serial->tx_data_count);
  1662. }
  1663. /* write callback for Diag and CS port */
  1664. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1665. {
  1666. struct hso_serial *serial = urb->context;
  1667. int status = urb->status;
  1668. struct tty_struct *tty;
  1669. /* sanity check */
  1670. if (!serial) {
  1671. D1("serial == NULL");
  1672. return;
  1673. }
  1674. spin_lock(&serial->serial_lock);
  1675. serial->tx_urb_used = 0;
  1676. tty = tty_kref_get(serial->tty);
  1677. spin_unlock(&serial->serial_lock);
  1678. if (status) {
  1679. log_usb_status(status, __func__);
  1680. tty_kref_put(tty);
  1681. return;
  1682. }
  1683. hso_put_activity(serial->parent);
  1684. if (tty) {
  1685. tty_wakeup(tty);
  1686. tty_kref_put(tty);
  1687. }
  1688. hso_kick_transmit(serial);
  1689. D1(" ");
  1690. return;
  1691. }
  1692. /* called for writing diag or CS serial port */
  1693. static int hso_std_serial_write_data(struct hso_serial *serial)
  1694. {
  1695. int count = serial->tx_data_count;
  1696. int result;
  1697. usb_fill_bulk_urb(serial->tx_urb,
  1698. serial->parent->usb,
  1699. usb_sndbulkpipe(serial->parent->usb,
  1700. serial->out_endp->
  1701. bEndpointAddress & 0x7F),
  1702. serial->tx_data, serial->tx_data_count,
  1703. hso_std_serial_write_bulk_callback, serial);
  1704. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1705. if (result) {
  1706. dev_warn(&serial->parent->usb->dev,
  1707. "Failed to submit urb - res %d\n", result);
  1708. return result;
  1709. }
  1710. return count;
  1711. }
  1712. /* callback after read or write on muxed serial port */
  1713. static void ctrl_callback(struct urb *urb)
  1714. {
  1715. struct hso_serial *serial = urb->context;
  1716. struct usb_ctrlrequest *req;
  1717. int status = urb->status;
  1718. struct tty_struct *tty;
  1719. /* sanity check */
  1720. if (!serial)
  1721. return;
  1722. spin_lock(&serial->serial_lock);
  1723. serial->tx_urb_used = 0;
  1724. tty = tty_kref_get(serial->tty);
  1725. spin_unlock(&serial->serial_lock);
  1726. if (status) {
  1727. log_usb_status(status, __func__);
  1728. tty_kref_put(tty);
  1729. return;
  1730. }
  1731. /* what request? */
  1732. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1733. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1734. D4("Actual length of urb = %d\n", urb->actual_length);
  1735. DUMP1(urb->transfer_buffer, urb->actual_length);
  1736. if (req->bRequestType ==
  1737. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1738. /* response to a read command */
  1739. serial->rx_urb_filled[0] = 1;
  1740. spin_lock(&serial->serial_lock);
  1741. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1742. spin_unlock(&serial->serial_lock);
  1743. } else {
  1744. hso_put_activity(serial->parent);
  1745. if (tty)
  1746. tty_wakeup(tty);
  1747. /* response to a write command */
  1748. hso_kick_transmit(serial);
  1749. }
  1750. tty_kref_put(tty);
  1751. }
  1752. /* handle RX data for serial port */
  1753. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1754. {
  1755. struct tty_struct *tty;
  1756. int write_length_remaining = 0;
  1757. int curr_write_len;
  1758. /* Sanity check */
  1759. if (urb == NULL || serial == NULL) {
  1760. D1("serial = NULL");
  1761. return -2;
  1762. }
  1763. /* All callers to put_rxbuf_data hold serial_lock */
  1764. tty = tty_kref_get(serial->tty);
  1765. /* Push data to tty */
  1766. if (tty) {
  1767. write_length_remaining = urb->actual_length -
  1768. serial->curr_rx_urb_offset;
  1769. D1("data to push to tty");
  1770. while (write_length_remaining) {
  1771. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1772. tty_kref_put(tty);
  1773. return -1;
  1774. }
  1775. curr_write_len = tty_insert_flip_string
  1776. (tty, urb->transfer_buffer +
  1777. serial->curr_rx_urb_offset,
  1778. write_length_remaining);
  1779. serial->curr_rx_urb_offset += curr_write_len;
  1780. write_length_remaining -= curr_write_len;
  1781. tty_flip_buffer_push(tty);
  1782. }
  1783. }
  1784. if (write_length_remaining == 0) {
  1785. serial->curr_rx_urb_offset = 0;
  1786. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1787. }
  1788. tty_kref_put(tty);
  1789. return write_length_remaining;
  1790. }
  1791. /* Base driver functions */
  1792. static void hso_log_port(struct hso_device *hso_dev)
  1793. {
  1794. char *port_type;
  1795. char port_dev[20];
  1796. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1797. case HSO_PORT_CONTROL:
  1798. port_type = "Control";
  1799. break;
  1800. case HSO_PORT_APP:
  1801. port_type = "Application";
  1802. break;
  1803. case HSO_PORT_GPS:
  1804. port_type = "GPS";
  1805. break;
  1806. case HSO_PORT_GPS_CONTROL:
  1807. port_type = "GPS control";
  1808. break;
  1809. case HSO_PORT_APP2:
  1810. port_type = "Application2";
  1811. break;
  1812. case HSO_PORT_PCSC:
  1813. port_type = "PCSC";
  1814. break;
  1815. case HSO_PORT_DIAG:
  1816. port_type = "Diagnostic";
  1817. break;
  1818. case HSO_PORT_DIAG2:
  1819. port_type = "Diagnostic2";
  1820. break;
  1821. case HSO_PORT_MODEM:
  1822. port_type = "Modem";
  1823. break;
  1824. case HSO_PORT_NETWORK:
  1825. port_type = "Network";
  1826. break;
  1827. default:
  1828. port_type = "Unknown";
  1829. break;
  1830. }
  1831. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1832. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1833. } else
  1834. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1835. dev2ser(hso_dev)->minor);
  1836. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1837. port_type, port_dev);
  1838. }
  1839. static int hso_start_net_device(struct hso_device *hso_dev)
  1840. {
  1841. int i, result = 0;
  1842. struct hso_net *hso_net = dev2net(hso_dev);
  1843. if (!hso_net)
  1844. return -ENODEV;
  1845. /* send URBs for all read buffers */
  1846. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1847. /* Prep a receive URB */
  1848. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1849. hso_dev->usb,
  1850. usb_rcvbulkpipe(hso_dev->usb,
  1851. hso_net->in_endp->
  1852. bEndpointAddress & 0x7F),
  1853. hso_net->mux_bulk_rx_buf_pool[i],
  1854. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1855. hso_net);
  1856. /* Put it out there so the device can send us stuff */
  1857. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1858. GFP_NOIO);
  1859. if (result)
  1860. dev_warn(&hso_dev->usb->dev,
  1861. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1862. i, result);
  1863. }
  1864. return result;
  1865. }
  1866. static int hso_stop_net_device(struct hso_device *hso_dev)
  1867. {
  1868. int i;
  1869. struct hso_net *hso_net = dev2net(hso_dev);
  1870. if (!hso_net)
  1871. return -ENODEV;
  1872. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1873. if (hso_net->mux_bulk_rx_urb_pool[i])
  1874. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1875. }
  1876. if (hso_net->mux_bulk_tx_urb)
  1877. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1878. return 0;
  1879. }
  1880. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1881. {
  1882. int i, result = 0;
  1883. struct hso_serial *serial = dev2ser(hso_dev);
  1884. if (!serial)
  1885. return -ENODEV;
  1886. /* If it is not the MUX port fill in and submit a bulk urb (already
  1887. * allocated in hso_serial_start) */
  1888. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1889. for (i = 0; i < serial->num_rx_urbs; i++) {
  1890. usb_fill_bulk_urb(serial->rx_urb[i],
  1891. serial->parent->usb,
  1892. usb_rcvbulkpipe(serial->parent->usb,
  1893. serial->in_endp->
  1894. bEndpointAddress &
  1895. 0x7F),
  1896. serial->rx_data[i],
  1897. serial->rx_data_length,
  1898. hso_std_serial_read_bulk_callback,
  1899. serial);
  1900. result = usb_submit_urb(serial->rx_urb[i], flags);
  1901. if (result) {
  1902. dev_warn(&serial->parent->usb->dev,
  1903. "Failed to submit urb - res %d\n",
  1904. result);
  1905. break;
  1906. }
  1907. }
  1908. } else {
  1909. mutex_lock(&serial->shared_int->shared_int_lock);
  1910. if (!serial->shared_int->use_count) {
  1911. result =
  1912. hso_mux_submit_intr_urb(serial->shared_int,
  1913. hso_dev->usb, flags);
  1914. }
  1915. serial->shared_int->use_count++;
  1916. mutex_unlock(&serial->shared_int->shared_int_lock);
  1917. }
  1918. if (serial->tiocmget)
  1919. tiocmget_submit_urb(serial,
  1920. serial->tiocmget,
  1921. serial->parent->usb);
  1922. return result;
  1923. }
  1924. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1925. {
  1926. int i;
  1927. struct hso_serial *serial = dev2ser(hso_dev);
  1928. struct hso_tiocmget *tiocmget;
  1929. if (!serial)
  1930. return -ENODEV;
  1931. for (i = 0; i < serial->num_rx_urbs; i++) {
  1932. if (serial->rx_urb[i]) {
  1933. usb_kill_urb(serial->rx_urb[i]);
  1934. serial->rx_urb_filled[i] = 0;
  1935. }
  1936. }
  1937. serial->curr_rx_urb_idx = 0;
  1938. serial->curr_rx_urb_offset = 0;
  1939. if (serial->tx_urb)
  1940. usb_kill_urb(serial->tx_urb);
  1941. if (serial->shared_int) {
  1942. mutex_lock(&serial->shared_int->shared_int_lock);
  1943. if (serial->shared_int->use_count &&
  1944. (--serial->shared_int->use_count == 0)) {
  1945. struct urb *urb;
  1946. urb = serial->shared_int->shared_intr_urb;
  1947. if (urb)
  1948. usb_kill_urb(urb);
  1949. }
  1950. mutex_unlock(&serial->shared_int->shared_int_lock);
  1951. }
  1952. tiocmget = serial->tiocmget;
  1953. if (tiocmget) {
  1954. wake_up_interruptible(&tiocmget->waitq);
  1955. usb_kill_urb(tiocmget->urb);
  1956. }
  1957. return 0;
  1958. }
  1959. static void hso_serial_common_free(struct hso_serial *serial)
  1960. {
  1961. int i;
  1962. if (serial->parent->dev)
  1963. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1964. tty_unregister_device(tty_drv, serial->minor);
  1965. for (i = 0; i < serial->num_rx_urbs; i++) {
  1966. /* unlink and free RX URB */
  1967. usb_free_urb(serial->rx_urb[i]);
  1968. /* free the RX buffer */
  1969. kfree(serial->rx_data[i]);
  1970. }
  1971. /* unlink and free TX URB */
  1972. usb_free_urb(serial->tx_urb);
  1973. kfree(serial->tx_data);
  1974. }
  1975. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1976. int rx_size, int tx_size)
  1977. {
  1978. struct device *dev;
  1979. int minor;
  1980. int i;
  1981. minor = get_free_serial_index();
  1982. if (minor < 0)
  1983. goto exit;
  1984. /* register our minor number */
  1985. serial->parent->dev = tty_register_device(tty_drv, minor,
  1986. &serial->parent->interface->dev);
  1987. dev = serial->parent->dev;
  1988. dev->driver_data = serial->parent;
  1989. i = device_create_file(dev, &dev_attr_hsotype);
  1990. /* fill in specific data for later use */
  1991. serial->minor = minor;
  1992. serial->magic = HSO_SERIAL_MAGIC;
  1993. spin_lock_init(&serial->serial_lock);
  1994. serial->num_rx_urbs = num_urbs;
  1995. /* RX, allocate urb and initialize */
  1996. /* prepare our RX buffer */
  1997. serial->rx_data_length = rx_size;
  1998. for (i = 0; i < serial->num_rx_urbs; i++) {
  1999. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2000. if (!serial->rx_urb[i]) {
  2001. dev_err(dev, "Could not allocate urb?\n");
  2002. goto exit;
  2003. }
  2004. serial->rx_urb[i]->transfer_buffer = NULL;
  2005. serial->rx_urb[i]->transfer_buffer_length = 0;
  2006. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2007. GFP_KERNEL);
  2008. if (!serial->rx_data[i]) {
  2009. dev_err(dev, "%s - Out of memory\n", __func__);
  2010. goto exit;
  2011. }
  2012. }
  2013. /* TX, allocate urb and initialize */
  2014. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2015. if (!serial->tx_urb) {
  2016. dev_err(dev, "Could not allocate urb?\n");
  2017. goto exit;
  2018. }
  2019. serial->tx_urb->transfer_buffer = NULL;
  2020. serial->tx_urb->transfer_buffer_length = 0;
  2021. /* prepare our TX buffer */
  2022. serial->tx_data_count = 0;
  2023. serial->tx_buffer_count = 0;
  2024. serial->tx_data_length = tx_size;
  2025. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2026. if (!serial->tx_data) {
  2027. dev_err(dev, "%s - Out of memory", __func__);
  2028. goto exit;
  2029. }
  2030. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2031. if (!serial->tx_buffer) {
  2032. dev_err(dev, "%s - Out of memory", __func__);
  2033. goto exit;
  2034. }
  2035. return 0;
  2036. exit:
  2037. hso_serial_common_free(serial);
  2038. return -1;
  2039. }
  2040. /* Frees a general hso device */
  2041. static void hso_free_device(struct hso_device *hso_dev)
  2042. {
  2043. kfree(hso_dev);
  2044. }
  2045. /* Creates a general hso device */
  2046. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2047. int port_spec)
  2048. {
  2049. struct hso_device *hso_dev;
  2050. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2051. if (!hso_dev)
  2052. return NULL;
  2053. hso_dev->port_spec = port_spec;
  2054. hso_dev->usb = interface_to_usbdev(intf);
  2055. hso_dev->interface = intf;
  2056. kref_init(&hso_dev->ref);
  2057. mutex_init(&hso_dev->mutex);
  2058. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2059. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2060. return hso_dev;
  2061. }
  2062. /* Removes a network device in the network device table */
  2063. static int remove_net_device(struct hso_device *hso_dev)
  2064. {
  2065. int i;
  2066. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2067. if (network_table[i] == hso_dev) {
  2068. network_table[i] = NULL;
  2069. break;
  2070. }
  2071. }
  2072. if (i == HSO_MAX_NET_DEVICES)
  2073. return -1;
  2074. return 0;
  2075. }
  2076. /* Frees our network device */
  2077. static void hso_free_net_device(struct hso_device *hso_dev)
  2078. {
  2079. int i;
  2080. struct hso_net *hso_net = dev2net(hso_dev);
  2081. if (!hso_net)
  2082. return;
  2083. /* start freeing */
  2084. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2085. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2086. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2087. }
  2088. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2089. kfree(hso_net->mux_bulk_tx_buf);
  2090. remove_net_device(hso_net->parent);
  2091. if (hso_net->net) {
  2092. unregister_netdev(hso_net->net);
  2093. free_netdev(hso_net->net);
  2094. }
  2095. hso_free_device(hso_dev);
  2096. }
  2097. /* initialize the network interface */
  2098. static void hso_net_init(struct net_device *net)
  2099. {
  2100. struct hso_net *hso_net = netdev_priv(net);
  2101. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2102. /* fill in the other fields */
  2103. net->open = hso_net_open;
  2104. net->stop = hso_net_close;
  2105. net->hard_start_xmit = hso_net_start_xmit;
  2106. net->tx_timeout = hso_net_tx_timeout;
  2107. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2108. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2109. net->type = ARPHRD_NONE;
  2110. net->mtu = DEFAULT_MTU - 14;
  2111. net->tx_queue_len = 10;
  2112. SET_ETHTOOL_OPS(net, &ops);
  2113. /* and initialize the semaphore */
  2114. spin_lock_init(&hso_net->net_lock);
  2115. }
  2116. /* Adds a network device in the network device table */
  2117. static int add_net_device(struct hso_device *hso_dev)
  2118. {
  2119. int i;
  2120. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2121. if (network_table[i] == NULL) {
  2122. network_table[i] = hso_dev;
  2123. break;
  2124. }
  2125. }
  2126. if (i == HSO_MAX_NET_DEVICES)
  2127. return -1;
  2128. return 0;
  2129. }
  2130. static int hso_radio_toggle(void *data, enum rfkill_state state)
  2131. {
  2132. struct hso_device *hso_dev = data;
  2133. int enabled = (state == RFKILL_STATE_ON);
  2134. int rv;
  2135. mutex_lock(&hso_dev->mutex);
  2136. if (hso_dev->usb_gone)
  2137. rv = 0;
  2138. else
  2139. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2140. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2141. USB_CTRL_SET_TIMEOUT);
  2142. mutex_unlock(&hso_dev->mutex);
  2143. return rv;
  2144. }
  2145. /* Creates and sets up everything for rfkill */
  2146. static void hso_create_rfkill(struct hso_device *hso_dev,
  2147. struct usb_interface *interface)
  2148. {
  2149. struct hso_net *hso_net = dev2net(hso_dev);
  2150. struct device *dev = &hso_net->net->dev;
  2151. char *rfkn;
  2152. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  2153. RFKILL_TYPE_WWAN);
  2154. if (!hso_net->rfkill) {
  2155. dev_err(dev, "%s - Out of memory\n", __func__);
  2156. return;
  2157. }
  2158. rfkn = kzalloc(20, GFP_KERNEL);
  2159. if (!rfkn) {
  2160. rfkill_free(hso_net->rfkill);
  2161. hso_net->rfkill = NULL;
  2162. dev_err(dev, "%s - Out of memory\n", __func__);
  2163. return;
  2164. }
  2165. snprintf(rfkn, 20, "hso-%d",
  2166. interface->altsetting->desc.bInterfaceNumber);
  2167. hso_net->rfkill->name = rfkn;
  2168. hso_net->rfkill->state = RFKILL_STATE_ON;
  2169. hso_net->rfkill->data = hso_dev;
  2170. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  2171. if (rfkill_register(hso_net->rfkill) < 0) {
  2172. kfree(rfkn);
  2173. hso_net->rfkill->name = NULL;
  2174. rfkill_free(hso_net->rfkill);
  2175. hso_net->rfkill = NULL;
  2176. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2177. return;
  2178. }
  2179. }
  2180. /* Creates our network device */
  2181. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  2182. {
  2183. int result, i;
  2184. struct net_device *net;
  2185. struct hso_net *hso_net;
  2186. struct hso_device *hso_dev;
  2187. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  2188. if (!hso_dev)
  2189. return NULL;
  2190. /* allocate our network device, then we can put in our private data */
  2191. /* call hso_net_init to do the basic initialization */
  2192. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2193. if (!net) {
  2194. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2195. goto exit;
  2196. }
  2197. hso_net = netdev_priv(net);
  2198. hso_dev->port_data.dev_net = hso_net;
  2199. hso_net->net = net;
  2200. hso_net->parent = hso_dev;
  2201. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2202. USB_DIR_IN);
  2203. if (!hso_net->in_endp) {
  2204. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2205. goto exit;
  2206. }
  2207. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2208. USB_DIR_OUT);
  2209. if (!hso_net->out_endp) {
  2210. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2211. goto exit;
  2212. }
  2213. SET_NETDEV_DEV(net, &interface->dev);
  2214. /* registering our net device */
  2215. result = register_netdev(net);
  2216. if (result) {
  2217. dev_err(&interface->dev, "Failed to register device\n");
  2218. goto exit;
  2219. }
  2220. /* start allocating */
  2221. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2222. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2223. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2224. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2225. goto exit;
  2226. }
  2227. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2228. GFP_KERNEL);
  2229. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2230. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2231. goto exit;
  2232. }
  2233. }
  2234. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2235. if (!hso_net->mux_bulk_tx_urb) {
  2236. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2237. goto exit;
  2238. }
  2239. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2240. if (!hso_net->mux_bulk_tx_buf) {
  2241. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2242. goto exit;
  2243. }
  2244. add_net_device(hso_dev);
  2245. hso_log_port(hso_dev);
  2246. hso_create_rfkill(hso_dev, interface);
  2247. return hso_dev;
  2248. exit:
  2249. hso_free_net_device(hso_dev);
  2250. return NULL;
  2251. }
  2252. static void hso_free_tiomget(struct hso_serial *serial)
  2253. {
  2254. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2255. if (tiocmget) {
  2256. kfree(tiocmget);
  2257. if (tiocmget->urb) {
  2258. usb_free_urb(tiocmget->urb);
  2259. tiocmget->urb = NULL;
  2260. }
  2261. serial->tiocmget = NULL;
  2262. }
  2263. }
  2264. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2265. static void hso_free_serial_device(struct hso_device *hso_dev)
  2266. {
  2267. struct hso_serial *serial = dev2ser(hso_dev);
  2268. if (!serial)
  2269. return;
  2270. set_serial_by_index(serial->minor, NULL);
  2271. hso_serial_common_free(serial);
  2272. if (serial->shared_int) {
  2273. mutex_lock(&serial->shared_int->shared_int_lock);
  2274. if (--serial->shared_int->ref_count == 0)
  2275. hso_free_shared_int(serial->shared_int);
  2276. else
  2277. mutex_unlock(&serial->shared_int->shared_int_lock);
  2278. }
  2279. hso_free_tiomget(serial);
  2280. kfree(serial);
  2281. hso_free_device(hso_dev);
  2282. }
  2283. /* Creates a bulk AT channel */
  2284. static struct hso_device *hso_create_bulk_serial_device(
  2285. struct usb_interface *interface, int port)
  2286. {
  2287. struct hso_device *hso_dev;
  2288. struct hso_serial *serial;
  2289. int num_urbs;
  2290. struct hso_tiocmget *tiocmget;
  2291. hso_dev = hso_create_device(interface, port);
  2292. if (!hso_dev)
  2293. return NULL;
  2294. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2295. if (!serial)
  2296. goto exit;
  2297. serial->parent = hso_dev;
  2298. hso_dev->port_data.dev_serial = serial;
  2299. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2300. num_urbs = 2;
  2301. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2302. GFP_KERNEL);
  2303. /* it isn't going to break our heart if serial->tiocmget
  2304. * allocation fails don't bother checking this.
  2305. */
  2306. if (serial->tiocmget) {
  2307. tiocmget = serial->tiocmget;
  2308. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2309. if (tiocmget->urb) {
  2310. mutex_init(&tiocmget->mutex);
  2311. init_waitqueue_head(&tiocmget->waitq);
  2312. tiocmget->endp = hso_get_ep(
  2313. interface,
  2314. USB_ENDPOINT_XFER_INT,
  2315. USB_DIR_IN);
  2316. } else
  2317. hso_free_tiomget(serial);
  2318. }
  2319. }
  2320. else
  2321. num_urbs = 1;
  2322. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2323. BULK_URB_TX_SIZE))
  2324. goto exit;
  2325. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2326. USB_DIR_IN);
  2327. if (!serial->in_endp) {
  2328. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2329. goto exit2;
  2330. }
  2331. if (!
  2332. (serial->out_endp =
  2333. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2334. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2335. goto exit2;
  2336. }
  2337. serial->write_data = hso_std_serial_write_data;
  2338. /* and record this serial */
  2339. set_serial_by_index(serial->minor, serial);
  2340. /* setup the proc dirs and files if needed */
  2341. hso_log_port(hso_dev);
  2342. /* done, return it */
  2343. return hso_dev;
  2344. exit2:
  2345. hso_serial_common_free(serial);
  2346. exit:
  2347. hso_free_tiomget(serial);
  2348. kfree(serial);
  2349. hso_free_device(hso_dev);
  2350. return NULL;
  2351. }
  2352. /* Creates a multiplexed AT channel */
  2353. static
  2354. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2355. int port,
  2356. struct hso_shared_int *mux)
  2357. {
  2358. struct hso_device *hso_dev;
  2359. struct hso_serial *serial;
  2360. int port_spec;
  2361. port_spec = HSO_INTF_MUX;
  2362. port_spec &= ~HSO_PORT_MASK;
  2363. port_spec |= hso_mux_to_port(port);
  2364. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2365. return NULL;
  2366. hso_dev = hso_create_device(interface, port_spec);
  2367. if (!hso_dev)
  2368. return NULL;
  2369. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2370. if (!serial)
  2371. goto exit;
  2372. hso_dev->port_data.dev_serial = serial;
  2373. serial->parent = hso_dev;
  2374. if (hso_serial_common_create
  2375. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2376. goto exit;
  2377. serial->tx_data_length--;
  2378. serial->write_data = hso_mux_serial_write_data;
  2379. serial->shared_int = mux;
  2380. mutex_lock(&serial->shared_int->shared_int_lock);
  2381. serial->shared_int->ref_count++;
  2382. mutex_unlock(&serial->shared_int->shared_int_lock);
  2383. /* and record this serial */
  2384. set_serial_by_index(serial->minor, serial);
  2385. /* setup the proc dirs and files if needed */
  2386. hso_log_port(hso_dev);
  2387. /* done, return it */
  2388. return hso_dev;
  2389. exit:
  2390. if (serial) {
  2391. tty_unregister_device(tty_drv, serial->minor);
  2392. kfree(serial);
  2393. }
  2394. if (hso_dev)
  2395. hso_free_device(hso_dev);
  2396. return NULL;
  2397. }
  2398. static void hso_free_shared_int(struct hso_shared_int *mux)
  2399. {
  2400. usb_free_urb(mux->shared_intr_urb);
  2401. kfree(mux->shared_intr_buf);
  2402. mutex_unlock(&mux->shared_int_lock);
  2403. kfree(mux);
  2404. }
  2405. static
  2406. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2407. {
  2408. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2409. if (!mux)
  2410. return NULL;
  2411. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2412. USB_DIR_IN);
  2413. if (!mux->intr_endp) {
  2414. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2415. goto exit;
  2416. }
  2417. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2418. if (!mux->shared_intr_urb) {
  2419. dev_err(&interface->dev, "Could not allocate intr urb?");
  2420. goto exit;
  2421. }
  2422. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2423. GFP_KERNEL);
  2424. if (!mux->shared_intr_buf) {
  2425. dev_err(&interface->dev, "Could not allocate intr buf?");
  2426. goto exit;
  2427. }
  2428. mutex_init(&mux->shared_int_lock);
  2429. return mux;
  2430. exit:
  2431. kfree(mux->shared_intr_buf);
  2432. usb_free_urb(mux->shared_intr_urb);
  2433. kfree(mux);
  2434. return NULL;
  2435. }
  2436. /* Gets the port spec for a certain interface */
  2437. static int hso_get_config_data(struct usb_interface *interface)
  2438. {
  2439. struct usb_device *usbdev = interface_to_usbdev(interface);
  2440. u8 config_data[17];
  2441. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2442. s32 result;
  2443. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2444. 0x86, 0xC0, 0, 0, config_data, 17,
  2445. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2446. return -EIO;
  2447. }
  2448. switch (config_data[if_num]) {
  2449. case 0x0:
  2450. result = 0;
  2451. break;
  2452. case 0x1:
  2453. result = HSO_PORT_DIAG;
  2454. break;
  2455. case 0x2:
  2456. result = HSO_PORT_GPS;
  2457. break;
  2458. case 0x3:
  2459. result = HSO_PORT_GPS_CONTROL;
  2460. break;
  2461. case 0x4:
  2462. result = HSO_PORT_APP;
  2463. break;
  2464. case 0x5:
  2465. result = HSO_PORT_APP2;
  2466. break;
  2467. case 0x6:
  2468. result = HSO_PORT_CONTROL;
  2469. break;
  2470. case 0x7:
  2471. result = HSO_PORT_NETWORK;
  2472. break;
  2473. case 0x8:
  2474. result = HSO_PORT_MODEM;
  2475. break;
  2476. case 0x9:
  2477. result = HSO_PORT_MSD;
  2478. break;
  2479. case 0xa:
  2480. result = HSO_PORT_PCSC;
  2481. break;
  2482. case 0xb:
  2483. result = HSO_PORT_VOICE;
  2484. break;
  2485. default:
  2486. result = 0;
  2487. }
  2488. if (result)
  2489. result |= HSO_INTF_BULK;
  2490. if (config_data[16] & 0x1)
  2491. result |= HSO_INFO_CRC_BUG;
  2492. return result;
  2493. }
  2494. /* called once for each interface upon device insertion */
  2495. static int hso_probe(struct usb_interface *interface,
  2496. const struct usb_device_id *id)
  2497. {
  2498. int mux, i, if_num, port_spec;
  2499. unsigned char port_mask;
  2500. struct hso_device *hso_dev = NULL;
  2501. struct hso_shared_int *shared_int;
  2502. struct hso_device *tmp_dev = NULL;
  2503. if_num = interface->altsetting->desc.bInterfaceNumber;
  2504. /* Get the interface/port specification from either driver_info or from
  2505. * the device itself */
  2506. if (id->driver_info)
  2507. port_spec = ((u32 *)(id->driver_info))[if_num];
  2508. else
  2509. port_spec = hso_get_config_data(interface);
  2510. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2511. dev_err(&interface->dev, "Not our interface\n");
  2512. return -ENODEV;
  2513. }
  2514. /* Check if we need to switch to alt interfaces prior to port
  2515. * configuration */
  2516. if (interface->num_altsetting > 1)
  2517. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2518. interface->needs_remote_wakeup = 1;
  2519. /* Allocate new hso device(s) */
  2520. switch (port_spec & HSO_INTF_MASK) {
  2521. case HSO_INTF_MUX:
  2522. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2523. /* Create the network device */
  2524. if (!disable_net) {
  2525. hso_dev = hso_create_net_device(interface);
  2526. if (!hso_dev)
  2527. goto exit;
  2528. tmp_dev = hso_dev;
  2529. }
  2530. }
  2531. if (hso_get_mux_ports(interface, &port_mask))
  2532. /* TODO: de-allocate everything */
  2533. goto exit;
  2534. shared_int = hso_create_shared_int(interface);
  2535. if (!shared_int)
  2536. goto exit;
  2537. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2538. if (port_mask & i) {
  2539. hso_dev = hso_create_mux_serial_device(
  2540. interface, i, shared_int);
  2541. if (!hso_dev)
  2542. goto exit;
  2543. }
  2544. }
  2545. if (tmp_dev)
  2546. hso_dev = tmp_dev;
  2547. break;
  2548. case HSO_INTF_BULK:
  2549. /* It's a regular bulk interface */
  2550. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2551. && !disable_net)
  2552. hso_dev = hso_create_net_device(interface);
  2553. else
  2554. hso_dev =
  2555. hso_create_bulk_serial_device(interface, port_spec);
  2556. if (!hso_dev)
  2557. goto exit;
  2558. break;
  2559. default:
  2560. goto exit;
  2561. }
  2562. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2563. /* save our data pointer in this device */
  2564. usb_set_intfdata(interface, hso_dev);
  2565. /* done */
  2566. return 0;
  2567. exit:
  2568. hso_free_interface(interface);
  2569. return -ENODEV;
  2570. }
  2571. /* device removed, cleaning up */
  2572. static void hso_disconnect(struct usb_interface *interface)
  2573. {
  2574. hso_free_interface(interface);
  2575. /* remove reference of our private data */
  2576. usb_set_intfdata(interface, NULL);
  2577. usb_driver_release_interface(&hso_driver, interface);
  2578. }
  2579. static void async_get_intf(struct work_struct *data)
  2580. {
  2581. struct hso_device *hso_dev =
  2582. container_of(data, struct hso_device, async_get_intf);
  2583. usb_autopm_get_interface(hso_dev->interface);
  2584. }
  2585. static void async_put_intf(struct work_struct *data)
  2586. {
  2587. struct hso_device *hso_dev =
  2588. container_of(data, struct hso_device, async_put_intf);
  2589. usb_autopm_put_interface(hso_dev->interface);
  2590. }
  2591. static int hso_get_activity(struct hso_device *hso_dev)
  2592. {
  2593. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2594. if (!hso_dev->is_active) {
  2595. hso_dev->is_active = 1;
  2596. schedule_work(&hso_dev->async_get_intf);
  2597. }
  2598. }
  2599. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2600. return -EAGAIN;
  2601. usb_mark_last_busy(hso_dev->usb);
  2602. return 0;
  2603. }
  2604. static int hso_put_activity(struct hso_device *hso_dev)
  2605. {
  2606. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2607. if (hso_dev->is_active) {
  2608. hso_dev->is_active = 0;
  2609. schedule_work(&hso_dev->async_put_intf);
  2610. return -EAGAIN;
  2611. }
  2612. }
  2613. hso_dev->is_active = 0;
  2614. return 0;
  2615. }
  2616. /* called by kernel when we need to suspend device */
  2617. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2618. {
  2619. int i, result;
  2620. /* Stop all serial ports */
  2621. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2622. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2623. result = hso_stop_serial_device(serial_table[i]);
  2624. if (result)
  2625. goto out;
  2626. }
  2627. }
  2628. /* Stop all network ports */
  2629. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2630. if (network_table[i] &&
  2631. (network_table[i]->interface == iface)) {
  2632. result = hso_stop_net_device(network_table[i]);
  2633. if (result)
  2634. goto out;
  2635. }
  2636. }
  2637. out:
  2638. return 0;
  2639. }
  2640. /* called by kernel when we need to resume device */
  2641. static int hso_resume(struct usb_interface *iface)
  2642. {
  2643. int i, result = 0;
  2644. struct hso_net *hso_net;
  2645. /* Start all serial ports */
  2646. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2647. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2648. if (dev2ser(serial_table[i])->open_count) {
  2649. result =
  2650. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2651. hso_kick_transmit(dev2ser(serial_table[i]));
  2652. if (result)
  2653. goto out;
  2654. }
  2655. }
  2656. }
  2657. /* Start all network ports */
  2658. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2659. if (network_table[i] &&
  2660. (network_table[i]->interface == iface)) {
  2661. hso_net = dev2net(network_table[i]);
  2662. if (hso_net->flags & IFF_UP) {
  2663. /* First transmit any lingering data,
  2664. then restart the device. */
  2665. if (hso_net->skb_tx_buf) {
  2666. dev_dbg(&iface->dev,
  2667. "Transmitting"
  2668. " lingering data\n");
  2669. hso_net_start_xmit(hso_net->skb_tx_buf,
  2670. hso_net->net);
  2671. hso_net->skb_tx_buf = NULL;
  2672. }
  2673. result = hso_start_net_device(network_table[i]);
  2674. if (result)
  2675. goto out;
  2676. }
  2677. }
  2678. }
  2679. out:
  2680. return result;
  2681. }
  2682. static void hso_serial_ref_free(struct kref *ref)
  2683. {
  2684. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2685. hso_free_serial_device(hso_dev);
  2686. }
  2687. static void hso_free_interface(struct usb_interface *interface)
  2688. {
  2689. struct hso_serial *hso_dev;
  2690. struct tty_struct *tty;
  2691. int i;
  2692. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2693. if (serial_table[i]
  2694. && (serial_table[i]->interface == interface)) {
  2695. hso_dev = dev2ser(serial_table[i]);
  2696. spin_lock_irq(&hso_dev->serial_lock);
  2697. tty = tty_kref_get(hso_dev->tty);
  2698. spin_unlock_irq(&hso_dev->serial_lock);
  2699. if (tty)
  2700. tty_hangup(tty);
  2701. mutex_lock(&hso_dev->parent->mutex);
  2702. tty_kref_put(tty);
  2703. hso_dev->parent->usb_gone = 1;
  2704. mutex_unlock(&hso_dev->parent->mutex);
  2705. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2706. }
  2707. }
  2708. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2709. if (network_table[i]
  2710. && (network_table[i]->interface == interface)) {
  2711. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2712. /* hso_stop_net_device doesn't stop the net queue since
  2713. * traffic needs to start it again when suspended */
  2714. netif_stop_queue(dev2net(network_table[i])->net);
  2715. hso_stop_net_device(network_table[i]);
  2716. cancel_work_sync(&network_table[i]->async_put_intf);
  2717. cancel_work_sync(&network_table[i]->async_get_intf);
  2718. if (rfk)
  2719. rfkill_unregister(rfk);
  2720. hso_free_net_device(network_table[i]);
  2721. }
  2722. }
  2723. }
  2724. /* Helper functions */
  2725. /* Get the endpoint ! */
  2726. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2727. int type, int dir)
  2728. {
  2729. int i;
  2730. struct usb_host_interface *iface = intf->cur_altsetting;
  2731. struct usb_endpoint_descriptor *endp;
  2732. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2733. endp = &iface->endpoint[i].desc;
  2734. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2735. (usb_endpoint_type(endp) == type))
  2736. return endp;
  2737. }
  2738. return NULL;
  2739. }
  2740. /* Get the byte that describes which ports are enabled */
  2741. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2742. {
  2743. int i;
  2744. struct usb_host_interface *iface = intf->cur_altsetting;
  2745. if (iface->extralen == 3) {
  2746. *ports = iface->extra[2];
  2747. return 0;
  2748. }
  2749. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2750. if (iface->endpoint[i].extralen == 3) {
  2751. *ports = iface->endpoint[i].extra[2];
  2752. return 0;
  2753. }
  2754. }
  2755. return -1;
  2756. }
  2757. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2758. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2759. struct usb_device *usb, gfp_t gfp)
  2760. {
  2761. int result;
  2762. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2763. usb_rcvintpipe(usb,
  2764. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2765. shared_int->shared_intr_buf,
  2766. shared_int->intr_endp->wMaxPacketSize,
  2767. intr_callback, shared_int,
  2768. shared_int->intr_endp->bInterval);
  2769. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2770. if (result)
  2771. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2772. result);
  2773. return result;
  2774. }
  2775. /* operations setup of the serial interface */
  2776. static const struct tty_operations hso_serial_ops = {
  2777. .open = hso_serial_open,
  2778. .close = hso_serial_close,
  2779. .write = hso_serial_write,
  2780. .write_room = hso_serial_write_room,
  2781. .ioctl = hso_serial_ioctl,
  2782. .set_termios = hso_serial_set_termios,
  2783. .chars_in_buffer = hso_serial_chars_in_buffer,
  2784. .tiocmget = hso_serial_tiocmget,
  2785. .tiocmset = hso_serial_tiocmset,
  2786. .unthrottle = hso_unthrottle
  2787. };
  2788. static struct usb_driver hso_driver = {
  2789. .name = driver_name,
  2790. .probe = hso_probe,
  2791. .disconnect = hso_disconnect,
  2792. .id_table = hso_ids,
  2793. .suspend = hso_suspend,
  2794. .resume = hso_resume,
  2795. .reset_resume = hso_resume,
  2796. .supports_autosuspend = 1,
  2797. };
  2798. static int __init hso_init(void)
  2799. {
  2800. int i;
  2801. int result;
  2802. /* put it in the log */
  2803. printk(KERN_INFO "hso: %s\n", version);
  2804. /* Initialise the serial table semaphore and table */
  2805. spin_lock_init(&serial_table_lock);
  2806. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2807. serial_table[i] = NULL;
  2808. /* allocate our driver using the proper amount of supported minors */
  2809. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2810. if (!tty_drv)
  2811. return -ENOMEM;
  2812. /* fill in all needed values */
  2813. tty_drv->magic = TTY_DRIVER_MAGIC;
  2814. tty_drv->owner = THIS_MODULE;
  2815. tty_drv->driver_name = driver_name;
  2816. tty_drv->name = tty_filename;
  2817. /* if major number is provided as parameter, use that one */
  2818. if (tty_major)
  2819. tty_drv->major = tty_major;
  2820. tty_drv->minor_start = 0;
  2821. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2822. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2823. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2824. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2825. tty_drv->init_termios = tty_std_termios;
  2826. hso_init_termios(&tty_drv->init_termios);
  2827. tty_set_operations(tty_drv, &hso_serial_ops);
  2828. /* register the tty driver */
  2829. result = tty_register_driver(tty_drv);
  2830. if (result) {
  2831. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2832. __func__, result);
  2833. return result;
  2834. }
  2835. /* register this module as an usb driver */
  2836. result = usb_register(&hso_driver);
  2837. if (result) {
  2838. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2839. result);
  2840. /* cleanup serial interface */
  2841. tty_unregister_driver(tty_drv);
  2842. return result;
  2843. }
  2844. /* done */
  2845. return 0;
  2846. }
  2847. static void __exit hso_exit(void)
  2848. {
  2849. printk(KERN_INFO "hso: unloaded\n");
  2850. tty_unregister_driver(tty_drv);
  2851. /* deregister the usb driver */
  2852. usb_deregister(&hso_driver);
  2853. }
  2854. /* Module definitions */
  2855. module_init(hso_init);
  2856. module_exit(hso_exit);
  2857. MODULE_AUTHOR(MOD_AUTHOR);
  2858. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2859. MODULE_LICENSE(MOD_LICENSE);
  2860. MODULE_INFO(Version, DRIVER_VERSION);
  2861. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2862. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2863. module_param(debug, int, S_IRUGO | S_IWUSR);
  2864. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2865. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2866. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2867. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2868. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2869. module_param(disable_net, int, S_IRUGO | S_IWUSR);